Reputation:
can someone explain to me what this code in a App.xaml does, especially the logic:AppDataModel part. AppDataModel is Class in the Project.
<Application.Resources>
<logic:AppDataModel
x:Key="TheViewModel" />
<x:String
x:Key="AppName">Master app</x:String>
</Application.Resources>
Upvotes: 1
Views: 47
Reputation: 50672
These xaml lines add items to the Resources dictionary of the current application:
Application.Current.Resources["TheViewModel"] = new logic.AppDataModel();
Application.Current.Resources["AppName"] = "Master app";
Upvotes: 0
Reputation: 4594
This markup, when parsed, creates two entries in the Application.Resources
dictionary. They key "TheViewModel"
is tied to a new instance of AppDataModel
and the key "AppName"
is tied to a string
initialized to "Mater app"
.
To go beyond your question, the reason you do this in XAML is to co-locate (keep together) your UI code and some instance data, loosely speaking. The biggest example is wanting your UI to always have a particular view model that it binds to. This can be achieved, as I assume from the markup you posted, like you're doing. Creating a view model object in the resources for a given control, window, or app and then assigning it using {StaticResource TheViewModel}
will keep you from having to muddy up your code-behind or your view model with binding code.
Hope this helps!
Upvotes: 1