wpf - Would a single resource file referenced in a window and at the app level have only one instance? -
i have loose form of mvvm have c# class containing resource data xaml bound to. file referenced in 2 places, mainwindow.xaml file , @ application level in app.xaml file. when referece in mainwindow code-behind there seems 1 instance , can set values in 1 instance seen in both window level , application level. can confirm or correct assumption? see edit below
i have mainwindow , mainwindowresource.cs file binding of mainwindow. resource file looks (shortened):
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 { { return _arrowbordercolor; } set { _arrowbordercolor = value; setpropertychanged("arrowbordercolor"); } } public color arrowcolor { { return _arrowcolor; } set { _arrowcolor = value; setpropertychanged("arrowcolor"); } } public bool viewfityaxis { { return _viewfityaxis; } set { _viewfityaxis = value; setpropertychanged("viewfityaxis"); } } public string viewfityaxistooltip { { 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 }
i reference mainwindowresouce file in mainwindow xaml:
<window.resources> <c:mainwindowresource x:key="mainwindowresource"/>
and bind properties of mainwindowresource within mainwindow xaml:
<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"/>
i have resource dictionary called arrowresources.xaml in define arrow paths. wished externalize values mainwindowresource file looks (shortened):
<resourcedictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:c="clr-namespace:userinterface"> <c:mainwindowresource x:key="mainwindowresource"/> <path x:key="arrowcounter" stroke="{binding source={staticresource mainwindowresource}, path=arrowbordercolor}" strokethickness="1" data="m 8,26 l 14,20 l 10,20 10,10 0 1 1 30,20 l 34,20 14,14 0 1 0 6,20 l 2,20 z"> <path.fill> <radialgradientbrush gradientorigin="0.15,0.2" center="0.3,0.4"> <gradientstop color="white" offset="0"/> <gradientstop color="{binding source={staticresource mainwindowresource}, path=arrowcolor}" offset="1"/> </radialgradientbrush> </path.fill> </path> </resourcedictionary>
the resource dictionary included in app.xaml this:
<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"> <application.resources> <resourcedictionary source="arrowresources.xaml"/> </application.resources> </application>
in code-behind of mainwindow aquire reference mainwindowresource class (shortened):
public partial class mainwindow : window { private mainwindowresource _mainwindowresource = null; public mainwindow() { initializecomponent(); // reference binding sources can set properties _mainwindowresource = (mainwindowresource)this.resources["mainwindowresource"]; } }
when reference appears there 1 instance of mainwindowresource class , if set values in it, changes reflected in both mainwindow.xaml , arrowresources.xaml @ application level.
can confirm this, or correct assumption?
edit
i wrong. arrowresources.xaml see values in mainwindowresource.cs, when instance of class inside mainwindow code-behind , change values arrows, arrow paths not recognize changes, must not same instance.
i tried create separate class file arrows , got instance of class in code-behind of mainwindow this:
private mainwindowresource _mainwindowresource = null; private arrowresource _arrowresource = null; . . . _mainwindowresource = (mainwindowresource)this.resources["mainwindowresource"]; _arrowresource = (arrowresource)application.current.resources["arrowresource"];
but when tried make changes values in arrowresource class (the backing class of resource dictionary in arrowresources.xaml ) change values still not reflected in arrow paths.
does have idea how make changes these values code-behind?
converting comment answer.
add public constructor mainwindowresource
class such as:
public mainwindowresource() { debug.writeline("called"); }
and see how many times it's called. guess two. tricky thing note redefining resource wouldn't result in new object creation until said resource used in child scope.
in case can remove resource duplicate definition mainwindow when it's declared in app.xaml(via resourcedictionary
), it's available mainwindow.xaml making redefinition pointless.
Comments
Post a Comment