4

I'm a newbie in windows phone development. I would like to ask if it is possible to do this scenario. I need to access a variable in XAML using my code behind, then I will add it as an item to my existing list found in my View Model. Therefore, I need to access both of my View Model to get the list and the XAML to get the variable from the resources.

Is this doable? If yes, how can I access it. This is what I have in my current XAML.

<phone:PhoneApplicationPage.Resources>
  <system:String x:Key="scanName">SCAN</system:String>
</phone:PhoneApplicationPage.Resources>

Thanks much,

JennyJane
  • 125
  • 6
  • 21

2 Answers2

1

I haven't near winphone app so i make simple example on wpf(it's similiar with winphone).

//write string value from dynamic resource into textblock

 <TextBlock FontSize="14" Text="{DynamicResource scanName}"/>

//changing resource in codebehind (this is Window in my example)

 this.Resources["scanName"] = "new value";

As my mind you scenario is veru specific.Try to read about bindings. May be bindings will be more useful in your scenario.

Frank59
  • 3,141
  • 4
  • 31
  • 53
1

What you're trying to do is a pretty big violation of everything MVVM is about, but it is possible...

With the following lines in the codebehind of your view, you can...

...access the resource string:

var scanName = this.Resources["scanName"];

...access the ViewModel:

var vm = DataContext as MyViewModel;
if (vm == null) return;
vm.ScanHistory.Add(scanName);

That being said, you really shouldn't do this. The idea of MVVM is to decouple ViewModel and View completely and let the WPF binding mechanisms wire it together for you. In your case, as far as I can tell, you should store the scan name somewhere else, either as a resource or a config value, fetch it in your ViewModel and provide a property on your ViewModel to which your View can bind.

Marc
  • 12,706
  • 7
  • 61
  • 97
  • :) Big violation, I was just asked to try this approach by my boss :( Anyway, how can I add the scanName string in the existing Observable collection list in my View Model (Scanhistory is the name of the list) I apologize for the questions, I'm really a newbie in c# and Windows development. Thanks again. – JennyJane Apr 15 '13 at 09:11
  • @JennyJane: I slightly updated my answer: To add the item to the list on your ViewModel, you need to parse the DataContext of your view to the type of the ViewModel and then you can acces the property... (vm.ScanHistory.Add(...)); – Marc Apr 15 '13 at 09:29
  • Hi @Marc Your a genius, it works! Thanks. :) I can't up vote due to lack of reputation. – JennyJane Apr 16 '13 at 07:43
  • Hi @Marc May I ask one last question. I tried the above code and the scanName was successfully added in the list. However, other items in the list is not loading anymore. Would you know the reason behind? – JennyJane Apr 16 '13 at 08:46
  • Hi @JennyJane, Have you tried setting a breakpoint on the vm.ScanHistory.Add(scanName) line? Check whether the list has any items at this point. Let me know... – Marc Apr 16 '13 at 10:08
  • Hello @Marc, Thanks for the patience. It has been resolved, i just did some modification in my View Model for relay command load data. Thanks again for your help. :) – JennyJane Apr 17 '13 at 03:22
  • By the way I did some modifications on your code, see below. It could help some beginners like me. Thanks again to you @Marc :) var vm = DataContext as MainViewModel; var scanName = this.Resources["scanName"]; Uri scanImage = new Uri("\\Resources\\Images\\Scan.png", UriKind.Relative); { HistoryItem scanHistory = new HistoryItem(); Product prod = new Product(); prod.Name = scanName.ToString(); prod.PictureUri = scanImage; scanHistory.ScanDateTime = DateTime.Now; scanHistory.ScannedProduct = prod; vm.ScanHistory.Add((HistoryItem)scanHistory); } – JennyJane Apr 17 '13 at 03:24
  • I have another question regarding this. How can I make sure that the code behind executes first before my View Model? Thanks – JennyJane Apr 17 '13 at 09:17
  • Can you create a new question, post some details and link it here? – Marc Apr 17 '13 at 10:53
  • Hello @Marc I have created a new question found here. http://stackoverflow.com/questions/16073614/which-executes-first-code-behind-or-view-model Thanks much again! – JennyJane Apr 18 '13 at 02:41
  • I see how it is a violation of MVVM, but when related to windowing is this justified? I have a closing/cleanup method in my ViewModel and when the window is instantiated, I grab the ViewModel from DataContext, and attach my closing/cleanup method from the ViewModel to the closing event on the window. Is this reasonable or is there a better way? – steviesama Jun 14 '15 at 05:34
  • @steviesama: It would be best to post a new question for this and give a few more details. I'll be happy to have a look at it, if you just post the link here... – Marc Jun 15 '15 at 10:02