A novice question about WPF.
I'm just at the beginning of a draft project.
I have define a really simple window testWindow1.xaml, located in the solution subfolder Tests.
In App.xaml I cannot do:
StartupUri="testWindow1.xaml"
(unless I move the testWindow1.xaml back to the root of the project)
I have also tried defining my namespace into the App.xaml tag, but without success, this wont work either.
<Application x:Class="MyProject.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:myprojectNS="clr-namespace:MyProject"
StartupUri="myprojectNS.tests.testWindow1.xaml">
At run time, the exception message complains about not finding the ressource *testWindow1.xaml
Try this -
<Application x:Class="MyProject.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:myprojectNS="clr-namespace:MyProject"
StartupUri="tests\testWindow1.xaml">
You just need to specify the hierarchy.
Related
I'm working on a generic 'window control' as I find I do a lot of the same things in the desktop apps I work on. I have a small project with a control, designed to go in a window in another project. This worked fine when in different projects (but the same solution), but now I have moved the window control to a new solution and packed it into a nuget, then referenced the nuget in my project with the window.
On initialising the window, it complains it can't find a resource that the control uses. For theming, when the user sets a theme (and on startup) an image which will best suit the brightness of the theme colour is picked and applied to a UI bound BitmapImage property. This is the resource that the usage of the control in my window in another project can't find, I get this error:
To me it seems the application I am using the control in is trying to find the pngs in its own resources, rather than that of the nuget. But I'm unsure on how to ensure the nuget functions only internally.
The two images to pick from are both set as 'Resource' and not to copy to output. I have also tried solutions where the images are simply copied to the output and read in via a relative path, however this doesn't seem to work:
On setting the theme, this code runs:
ThemeImage = isThemeDark
? new BitmapImage(new Uri(#"pack://application:,,,/Images/Pallete_Light.png", UriKind.Absolute))
: new BitmapImage(new Uri(#"pack://application:,,,/Images/Pallete_Dark.png", UriKind.Absolute));
This works fine on a test window in the same project as the control, the little colour pallete icon is the on being picked from:
My window is very simple, and is the same as the window from the project where it works.
<Window
x:Class="SomeApplicationName.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SomeApplicationName" xmlns:win10themables="clr-namespace:Win10Themables;assembly=Win10Themables"
Title="SomeApplicationName"
Width="800"
Height="450"
AllowsTransparency="True"
Background="Transparent"
ResizeMode="NoResize"
WindowStyle="None"
WindowState="{Binding ElementName=WindowControl, Path=DataContext.WindowStateProperty, UpdateSourceTrigger=PropertyChanged}">
<win10themables:MainWindowControl x:Name="WindowControl" Title="SomeApplicationName" />
</Window>
In my App.xaml, I simply merge some resource dictionaries from the project the window control comes from so this project can use them:
<Application
x:Class="SomeApplicationName.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>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Win10Themables;component/ResourceDictionaries/Styles.xaml" />
<ResourceDictionary Source="/Win10Themables;component/ResourceDictionaries/Converters.xaml" />
<ResourceDictionary Source="/Win10Themables;component/ResourceDictionaries/Brushes.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Sorry for the code dump. I'm at a bit of a loss on how to resolve this. I wanted to avoid repeatedly copying the base window project into each of my new projects, but at this point might be forced to do so anyway.
To anyone else who runs into this issue, I resolved this by keeping the images as resources, but then adding the images to a resource dictionary and merging that into the app.xaml of the project with the control, and referencing the image through Application.Current.Resource["ImageResourceName"] and casting to the type of my property (BitmapImage).
In my application where I consume this nuget (and add the control to the window), I also merge this resource dictionary into my app.xaml. This seems to resolve the issue, though I can't entirely understand the difference between referring it via the projects resources, and the resource dictionary, and why the latter resolves this. If anyone viewing this with this answer has any light to shed on this for mine and anyone elses benefit, please do!
I've built a WPF app, which completely works now. However, to clean it up a bit I wish to move my MainWindow.xaml to the view folder I've created. After I've done this the application won't run and it gives me an "Unknown build error" which doesn't give any info on how to fix it...
What should I change in my MainWindow.xaml to let the app work properly again?
I've already changed
<Window x:Class="projectname.MainWindow">
to
<Window x:Class="projectname.view.MainWindow">
Should I change other stuff as well?
You don't need to update class name in xaml file unless you have changed the namespace of your class.
Most likely you haven't updated StartupUri for App.xaml. Change it to:
StartupUri="view/MainWindow.xaml"
from
StartupUri="MainWindow.xaml"
I just ran into this myself. Here's what I did:
Move your MainWindow.xaml to your new folder. In my case it was /Views.
I like to name all my classes with their namespaces reflecting their folder. So my MainWindow.xaml.cs namespace went from ProjectNamespace to ProjectNamespace.Views
In my MainWindow.xaml, I needed to change x:Class from ProjectName.MainWindow to ProjectName.Views.MainWindow.
In your App.xaml, change the StartupUri to reflect the folder. I went from StartupUri="MainWindow.xaml" to "StartupUri="Views/MainWindow.xaml"`
Doing this allowed me to compile and run my app.
The answer of #Rohit Vats is quite good!
However there's a good point to remember: you have to change all the absolute paths (in XAML) to your resources, prepending them by a / in order to indicate that these paths are relative to the root directory.
Example:
From <Image Source="Assets/Loading.gif">
To <Image Source="/Assets/Loading.gif"> and so on.
I'm having trouble with the code below:
<Window x:Class="ChangePage.PageSwitcher"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ChangePage"
Title="ECE Showcase"
WindowState="Maximized">
<Window.Resources>
<local:PageSwitcher x:Name="pageTransitionControl" TransitionType="SlideAndFade"/>
</Window.Resources>
In the code behind file PageSwitcher.xaml.cs I have the following line:
pageTransitionControl.TransitionType = whatever;
However this results in the following error:
The name 'pageTransitionControl' does not exist in the current context
I've been searching around the Internet for a few hours now trying to find a reason for this but haven't been able to figure it out yet. Build Action is set to Page, all files are saved, I've tried rebuilding, PageSwitcher is in the ChangePage namespace and PageSwitcher has a constructor.
Is there something else I am doing wrong?
You cannot assign names to resources. Resources have keys.
<Window x:Class="ChangePage.PageSwitcher"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ChangePage"
Title="ECE Showcase"
WindowState="Maximized">
<Window.Resources>
<local:PageSwitcher x:Key="pageTransitionControl" TransitionType="SlideAndFade"/>
</Window.Resources>
Then in the xaml.cs:
var pageTransitionControl = (PageSwitcher)Resources["pageTransitionControl"];
pageTransitionControl.TransitionType = whatever;
The item exists in the Window's Resources, not in the Window itself
You can either place it directly in the Window itself instead of it's Resources to make your code work, or assign it an x:Key instead of an x:Name and get it using
(PageSwitcher)this.Resources["pageTransitionControl"]
Seems you cannot directly access the resources in the code behind. You may need to use Resources["pageTransitionControl"] to access it.
This might be an extremely dumb question, but I simply can't understand the problem at the moment.
I have an App.xaml file that defines application level resources :
<Application x:Class="WpfMPManager.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>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MyDictionnary.xaml"/>
</ResourceDictionary>
</Application.Resources>
For various reasons, I start this application through a .cs file like that (there actually is a lot more going on in this file).
[STAThread]
static void Main(params string[] args)
{
App myApp = new App();
MainWindow myWindow = new MainWindow();
myApp.Run(myWindow);
}
However, when I start the application in this way, my application resource dictionnaries are empty (and they are indeed filled if I start the application through my App.xml).
Should I call a specific method on the Application object to force it to load the resource dictionaries defined in the .xaml file ?
Thanks in advance.
Answering my own question in case anyone stumbles on that
It seems it's enough to call myApp.InitializeComponent() which is not called by the default constructor. Could have figured it out faster.
Im new to Expression and by accident I deleted the App.xaml file. I think this is an important file and I cannot workout how to create an equivalent.
Please help,
Andy
Create a new project and copy that one.
<Application x:Class="Test.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml">
<Application.Resources>
</Application.Resources>
</Application>
Unless you had Application Resources defined, then you may be in trouble.
Might be a good argument for Source Control.
You can create a new Page and call it App.xaml.
Replace its markup as benPearce indicated with this:
<Application x:Class="Test.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml">
<Application.Resources>
</Application.Resources>
</Application>
Replace "Test" above with your namespace and "Window1" with the name of the first page you want shown in your project.
Replace the class in the App.xaml.cs code-behind with this:
public partial class App : Application
{
[STAThread]
public static void Main()
{
YourNamespace.App app = new YourNamespace.App();
app.InitializeComponent();
app.Run();
}
}
Ensure that your Project Properties are set in the "Application" area such that your startup object is YourNamespace.App.
Perform a build and you shouldn't get anymore errors related to 'App'.