10

I have several resources declared in XAML markup and would like to access the resources in my view model. I could easily use something foo.Resources["resourceKey"] or findResource("") but that would mean that I would have to couple my XAML and C# code closely together. Not what I intended to do when I started using MVVM.

Is there a best practice or any way to use resources defined in the view in the view model?

Update:

The resources are mainly symbol definitions which are used to define the appearance of symbols in ESRI maps. For example this:

<esri:MarkerSymbol x:Key="SMS">
    <esri:MarkerSymbol.ControlTemplate>
        <ControlTemplate>
            <Ellipse x:Name="Element" Margin="-7,-7,0,0" Width="14" Height="14" Fill="Blue">
                <VisualStateManager.VisualStateGroups>
                    <VisualStateGroup x:Name="SelectionStates">
                        <VisualState x:Name="Unselected" />
                        <VisualState x:Name="Selected">
                            <Storyboard>
                                <ColorAnimation Storyboard.TargetName="Element" Storyboard.TargetProperty="(Ellipse.Fill).(SolidColorBrush.Color)" To="Green" Duration="00:00:0.25"/>
                            </Storyboard>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateManager.VisualStateGroups>
            </Ellipse>
        </ControlTemplate>
    </esri:MarkerSymbol.ControlTemplate>
</esri:MarkerSymbol>

The symbols are added programatically to the map, though I need to access them in my view model.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Jay
  • 2,141
  • 6
  • 26
  • 37
  • 3
    Exactly what resources are you defining in your XAML markup that you need to access in the view-model? – Luke Woodward Jul 17 '11 at 21:07
  • Why do you need to access these symbols in the view-model? – Luke Woodward Jul 18 '11 at 13:08
  • Because I need to add symbols at runtime, depending on the data a user selects from database. – Jay Jul 18 '11 at 16:31
  • Is this so you can use the [Draw](http://resources.arcgis.com/en/help/silverlight-api/apiref/api_start.htm?ESRI.ArcGIS.Client~ESRI.ArcGIS.Client.Draw.html) object? If so, maybe a more appropriate question would be "How can I define the Draw object in XAML?". – Kirk Kuykendall Sep 29 '12 at 21:20

3 Answers3

11

If you really want to access styles/templates from the view-model layer, one thing you can do is to put the styles in a separate ResourceDictionary (let's suppose you called it SymbolTemplates.xaml) and load that in:

var resourceDictionary = new ResourceDictionary()
{
    Source = new Uri("SymbolTemplates.xaml", UriKind.Relative)
};

ControlTemplate template = resourceDictionary["SMS"] as ControlTemplate;

(I found that this approach works best if you set the Build Action for SymbolTemplates.xaml to Content.)

A more MVVM-ish approach would be to have the view-model layer expose the 'type' of each symbol. This type would determine the template or style to apply to each symbol added to the view. You'd then use an IValueConverter to convert the type of symbol into the style or template to apply. This value-converter would need to load the resource dictionary, but since value converters live in the view-layer, that doesn't break MVVM.

Luke Woodward
  • 63,336
  • 16
  • 89
  • 104
6

First think you would want to ask yourself is, why would you want to access your XAML resources from the VM.

There are many ways to access and manipulate the XAML resources from the VM, the question is again, why would you want to do that.

Because for example, if you want to run an animation, you would be better off trigger an event from the VM and run the anim. on the UI without the VM to know about this particular animation (Check out this for examples).

The goal of ViewModel is to keep it UI-independent so do your maximum to decouple them.

Community
  • 1
  • 1
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
0

You can get your application-wide xaml resources

Application.Current.Resources["ApplicationScopeResource"] = Brushes.White;
Mohamed Elhakim
  • 204
  • 1
  • 3
  • 7