I'm facing a problem that I probably can't solve on my own, I'm working on a solution now for 2 days and browsed a lot in other Forums etc. - but it seems like that I just don't get it.
My problem
I have a Storyboard called "getVisible" in my UserControl "MainPage" that I want to access in another class called "test.cs"
However, as the Storyboard is defined in the MainPage.xaml and only the App Resources are accessible, do I face the problem that I can't load Storyboards from another class.
Any ideas how to solve that?
~regards Matt
One option to get the storyboard from test.cs is to create an instance of MainPage.
(new MainPage()).Resources["getVisible"];
Alternatively though, you may wish to consider creating the resources in a separate resource dictionary and then referencing that shared resource dictionary from both loactions.
Related
I have a wpf application where I want to hide some of the code complexity by creating resource dictionaries.
I have an external file called "SubFolder\LoginPanel.xaml".
I created it as a resource dictionary, and added the reference to the App.xaml file.
In LoginPanel.xaml I have some custom colors defined, brushes, and two storyboards, sbShowLoginMenu, sbHideLoginMenu.
In my MainWindow.xaml I can reference the defined brushes without any problems. However, I want to access my storyboards as well, but from C# code behind for mouse enter.
If I copy the story boards to the MainWindow.xaml file the storyboard works correctly, if I remove it from MainWindow.xaml it does not see the resource. It only sees the resources defined in the MainWindow.xaml.
My question is how do I get my code to work so that I can access the storyboard items from the resource dictionary?
My guess is that I have to add something to the 'Window' tag such as xmlns:rd="clr-namespace:LoginPanel". I know this is not the right syntax but I could not find any documentation for this type of problem.
EDIT: When I try var sb = FindResource("sbShowLoginMenu"), the debugger returns 'resource not found.'.
Well, finally I was able to craft a search criteria that gives the right answer.
Storyboards in ResourceDictionary
Basically storyboards are stateful objects and cannot be placed in a resource dictionary.
I use Catel and DevExpress DockLayoutManager in my application. I wanted to use this code to automatically create a View Model:
http://gyazo.com/147dff382d16e08cee0270ac20c6b330
http://gyazo.com/24a4ec62e90d49b4e08e3ba1d1790b59
but I can not run the application after compilation.
If we replace catel:UserControl on UserControl then everything works fine:
http://gyazo.com/b69a0257d992bb13c19813c055d47d92
, but goes without is not created ViewModel.
I wanted to use IUserControl but it has not helped. UserControlLogic does not perceive DocumentPanel as UserControl:
http://gyazo.com/e5596231404c054e459db06446ab57ee.
The reason for this is that DocumentPanelEx (and thus DocumentPanel) does not derive from UserControl. I will investigate if the base class being used can be turned into ContentControl instead of UserControl (but then the question is: from which class does DocumentPanel derive?). If you want us to investigate, please add an issue to the backlog at http://www.catelproject.com/support/issue-tracker/
For now you should put a UserControl as root into the DocumentPanelEx. I know it's not the ideal situation, but it will solve your problems for now.
I've reviewed your project from ticket Catel and DevExpress DockLayoutManager don't work together and it seems that the issue is caused by the Catel UserControl. On startup it tries to recursively find the InfoBarMessageControl in the visual and logical trees. If there is no such control and the visual tree is large, this operation may take a long time. I suggest you wrap the DockLayoutManager in the InfoBarMessageControl, this should resolve the issue.
I have a program in visual basic .net and WPF that uses a series of pages displayed in a Frame control on my main Window, pages are navigated from controls in the main Window outside of the frame and I have all that working ok.
I also have a MediaElement control on that main Window and I need to be able to change the source property of this control from the user clicking on elements in the pages. Every time I have tried to do this so far i have run into an error.
Right now I have a Public Shared function within main Window that is called from the control in the page, the code on the control passes on the URL to be loaded into the source property to the function, then the function is supposed to pass the url into the source property and tell the MediaElement to play.
The error I am getting when trying to achieve this is:
Cannot refer to an instance member of a class from within a shared
method or shared member initializer without an explicit instance of
the class.
Please help, how could I achive this?
Are you getting an instance of the main window or the media element inside of your shared function? If not, this is you problem.
You might stick the media element into a shared variable when the main window is loaded or you can access your main window using the rootvisual of your application.current.app.rootvisual.
If your issue is something else, please post the code in your shared function that causes the error.
Thank you very much Steve for your answer, however I found how to do what I need on another post after idly trying some different search terms today.
Someone known as Jean on another post: https://stackoverflow.com/a/16781963/2668533 suggested something that worked for me, by using application.current.windows:
Dim parent As MainWindow = Application.Current.Windows(0)
parent.player.Source = New Uri(url)
Apologies for seeming such a novice but WPF is completely new to me and I have never encounterd a need for anything similer using WindowsForms.
We're using ResourceDictionaries to store layout information for our app. The lazy initialisation is great because we can grab the required layout for an element only when that element is created. The problem is that each element gets the same instance from the ResourceDictionary for each one that shares the layout (or the same one if it goes out of scope and is needed later).
We could store templates in the dictionary then it doesn't matter if they're shared but we have quite a nice system going if it wasn't for this caching behaviour.
Is there a way to change this behaviour of the dictionary or am I going to have to write my own class which creates items from the XAML on demand?
Try applying x:Shared="false" attribute. Each resource should then get seperate copy.
I have a problem with creating the instance of a class in xaml file. I thought you can do it like this:
<src:MyDataClass x:Key="data"/>
in the resource part of the user control and then use it in the xaml file (for example bind to it).
But even tough the class I created is located in the same namespace it says that:
"The type was not found. Verfiy that all assemblies were built, etc".
How to get it right? is there another method?
Thanks for suggestions.
Well obviously you have to add the right namespace to your xaml. The namespace which contains the class you want instantiate. After reading this, you should be able to solve your problem.