Note that there are some explanatory texts on larger screens.

plurals
  1. POWould a single resource file referenced in a window and at the app level have only one instance?
    primarykey
    data
    text
    <p>I have loose form of MVVM where I have a C# class containing the resource data that my XAML is bound to. The file is referenced in two places, the MainWindow.xaml file and at the application level in the App.xaml file. When I get a referece to it in the MainWindow code-behind there seems to be only one instance and I can set values in that one instance that are seen in both the window level and the application level. Can anyone confirm or correct this assumption? <strong>see edit below</strong></p> <p>I have a MainWindow and a MainWindowResource.cs file to back the binding of the MainWindow. The resource file looks like this (shortened):</p> <pre><code>public class MainWindowResource : INotifyPropertyChanged { #region Data Members private Color _arrowBorderColor = Colors.Black; private Color _arrowColor = Colors.Black; private bool _viewFitYAxis = true; private string _viewFitYAxisTooltip = ""; #endregion Data Members #region Properties public Color ArrowBorderColor { get { return _arrowBorderColor; } set { _arrowBorderColor = value; SetPropertyChanged("ArrowBorderColor"); } } public Color ArrowColor { get { return _arrowColor; } set { _arrowColor = value; SetPropertyChanged("ArrowColor"); } } public bool ViewFitYAxis { get { return _viewFitYAxis; } set { _viewFitYAxis = value; SetPropertyChanged("ViewFitYAxis"); } } public string ViewFitYAxisTooltip { get { return _viewFitYAxisTooltip; } set { _viewFitYAxisTooltip = value; SetPropertyChanged("ViewFitYAxisTooltip"); } } #endregion Properties #region INotifyPropertyChanged implementation public event PropertyChangedEventHandler PropertyChanged; private void SetPropertyChanged(string prop) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(prop)); } } #endregion INotifyPropertyChanged implementation } </code></pre> <p>I reference the MainWindowResouce file in the MainWindow XAML:</p> <pre><code>&lt;Window.Resources&gt; &lt;c:MainWindowResource x:Key="MainWindowResource"/&gt; </code></pre> <p>And I bind to properties of the MainWindowResource within the MainWindow XAML:</p> <pre><code>&lt;MenuItem x:Name="MenuFitYAxis" Header="Fit _Y Axis" IsCheckable="True" IsChecked="{Binding Source={StaticResource MainWindowResource}, Path=ViewFitYAxis}" ToolTip="{Binding Source={StaticResource MainWindowResource}, Path=ViewFitYAxisTooltip}" Click="MenuItem_Click_ViewFitYAxis"/&gt; </code></pre> <p>I also have a resource dictionary called ArrowResources.xaml in which I define some arrow paths. I also wished to externalize some values into the MainWindowResource file so it looks like this (shortened):</p> <pre><code>&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:c="clr-namespace:UserInterface"&gt; &lt;c:MainWindowResource x:Key="MainWindowResource"/&gt; &lt;Path x:Key="ArrowCounter" Stroke="{Binding Source={StaticResource MainWindowResource}, Path=ArrowBorderColor}" StrokeThickness="1" Data="M 8,26 L 14,20 L 10,20 A 10,10 0 1 1 30,20 L 34,20 A 14,14 0 1 0 6,20 L 2,20 Z"&gt; &lt;Path.Fill&gt; &lt;RadialGradientBrush GradientOrigin="0.15,0.2" Center="0.3,0.4"&gt; &lt;GradientStop Color="White" Offset="0"/&gt; &lt;GradientStop Color="{Binding Source={StaticResource MainWindowResource}, Path=ArrowColor}" Offset="1"/&gt; &lt;/RadialGradientBrush&gt; &lt;/Path.Fill&gt; &lt;/Path&gt; &lt;/ResourceDictionary&gt; </code></pre> <p>The resource dictionary is included in my App.xaml like this:</p> <pre><code>&lt;Application x:Class="UserInterface.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="MainWindow.xaml"&gt; &lt;Application.Resources&gt; &lt;ResourceDictionary Source="ArrowResources.xaml"/&gt; &lt;/Application.Resources&gt; &lt;/Application&gt; </code></pre> <p>In the code-behind of my MainWindow I aquire a reference to the MainWindowResource class like this (shortened):</p> <pre><code>public partial class MainWindow : Window { private MainWindowResource _mainWindowResource = null; public MainWindow() { InitializeComponent(); // get a reference to the binding sources so we can set the properties _mainWindowResource = (MainWindowResource)this.Resources["MainWindowResource"]; } } </code></pre> <p>When I get this reference it appears that there is only one instance of the MainWindowResource class and if I set values in it, those changes will be reflected in both the MainWindow.xaml and the ArrowResources.xaml at the application level.</p> <p>Can anyone confirm this, or correct my assumption?</p> <p><strong>Edit</strong></p> <p>I was wrong. The ArrowResources.xaml does see the values in MainWindowResource.cs, but when I get an instance of the class inside the MainWindow code-behind and I change the values for the arrows, the arrow paths do not recognize the changes, so it must not be the same instance.</p> <p>I tried to create a separate class file for the arrows and I got an instance of that class in the code-behind of the MainWindow like this:</p> <pre><code> private MainWindowResource _mainWindowResource = null; private ArrowResource _arrowResource = null; . . . _mainWindowResource = (MainWindowResource)this.Resources["MainWindowResource"]; _arrowResource = (ArrowResource)Application.Current.Resources["ArrowResource"]; </code></pre> <p>But when I tried to make changes to the values in the ArrowResource class (the backing class of the resource dictionary in ArrowResources.xaml ) I could change the values but they were still not reflected in the arrow paths.</p> <p>Does anyone have any idea how to make changes to these values from code-behind?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COAdd a public constructor to `MainWindowResource` class and add a `Debug.WriteLine("Called");` and see how many times it's called. My guess is two. Why do you want to redefine that resource in MainWindow. If it's part of the ResourceDictionary which gets added at application scope, it's already available to MainWindow.xaml making the redefinition pointless
      singulars
    2. COViv, thanks for your reply. That makes sense. I created a problem where the shouldn't be one. I originally had all my resources in the MainWindow and it was getting too big so I peeled off some of them and created the resource dictionary. The resouces in the dictionary needed to access the MainWindowResource class so I added that to the application level not thinking that I could now just remove it from the window level. Now I only have it referenced in the App.xaml and I can still reference it in the code-behind so all is good.
      singulars
    3. COViv, I still have the issue of the arrow path data not being recognized, but that is a different issue I can tackle on my own. - If you will post this as an answer, I will mark it "answered" and give you credit.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload