Sorry if my question is confusing what im trying to ask is how can i render a page that is located in a folder
for example:
my MainWindow.xaml is located in the root folder
i want to render a page located in "ProjectName/Pages" inside a
In case you are still confused this
ProjectName
-- Pages <--- The page i want to render is in this folder
-- App.config
-- App.xaml
-- MainWndow.xaml <--- i want to render the page here
i dont want to have both page and window in the root of the folder, it will look too messy
Add a new Page to your "Pages" folder. Right-click on the folder in the Solution Explorer in Visual Studio and select Add->New Item and then choose a Page and call it "Page1.xaml" or whatever.
You could then display the Page in your window using a Frame like this:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="Window12" Height="300" Width="300">
<StackPanel>
<Frame x:Name="frame" Source="/Pages/Page1.xaml" />
</StackPanel>
</Window>
Related
Yesterday I create a project in my disk D partition and I found the images I put into project can't be displayed well. XAML code below is very simple:
<Window x:Class="WpfImageTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfImageTest"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Image Width="300" Height="300" Source="/Resources/Images/Chen.png"/>
</Grid>
</Window>
You can see I write a relative path for Image control's Source property and when I run the application it shows nothing on the interface.But magically I copy all the files into the root of my C partition or D partition of my physical disk and rebuild the project, the application runs properly. This proplem troubles me almost all day , can anyone explain why this thing happens?
The original location of my project is D:\Programming\C#\Practice\WpfImageTest and I have already set all images build property to Copy all the time and Content.
Select your image file in your Solution Browser and set its Build Action to Resource as on image below.
I have made several ResourceDictionaries for our applications team to use with their future applications. I have deployed the contents of the class library project containing these dictionaries to a .dll file and would like to be able to use the dictionaries by adding a reference to the .dll file in a new WPF solution where I hope to make a new application.
The Class Library in my example is called "NWF_Class_Library.dll" and is saved in the same folder in windows explorer as the MainWindow.xaml file. Is it possible to retrieve the resource dictionaries from within it?
I have read articles about the best way for an organisation to arrange their xaml resources, so it seems it must be possible, but all I find is ways to use the "//pack:application:..." syntax to reference xaml within the same solution as the wpf application. Here is a snippet of code, with the Source blank because nothing I have written has worked!
We had hoped that we could add the standard configurations as well as our more normal useful methods etc to a file that can be deployed with applications.
<Window x:Class="dll_ref_included.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<ResourceDictionary Source=""/>
</Window.Resources>
<Grid>
<Button Style="{StaticResource myButton}">This</Button>
</Grid>
</Window>
Try this:
<ResourceDictionary Source="pack://application:,,,/NWF_Class_Library;component/Dictionary1.xaml"/>
...where "NWF_Class_Library" is the name of the referenced assembly and "Dictionary1.xaml" is the name of the ResourceDictionary that is defined in this project.
You can refer to the documentation for more information about pack URIs and how to use them.
Why can't my custom ListView have it's own xaml file? I have a custom Button and it works with a xaml file with no issues, but not my ListView. The main reason I want to use this approach (rather than be forced to create a Style that is place in the Generic.xaml file) is because I would like to take advantage of the Resources element and place all resources related to the listview within the xaml file:
public sealed partial class MyListView : ListView
{
public MyListView()
{
this.InitializeComponent();
}
}
And here is the associated xaml file:
<ListView
x:Class="App1.MyListView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<ListView.Resources>
<!-- I would like to place all related resources here instead of having
them placed in external locations, and then have to open different files to find them. -->
</ListView.Resources>
</ListView>
Although I would expect this should work as well, it seems that this problem is present and it is recommended to use the templated control instead.
I suppose the problem is that assigning the compiler is unable to generate the valid assignment to the Items property of the control, which it is trying to construct from the content of the element. Even when the element is closed immediately it seems to be an issue.
Why not place resources on the Page or inside ListView, rather than deriving your own control?
<Page
x:Class="ListViewResources.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ListViewResources"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
<!-- Place all related resources here instead of having them placed in external locations, and then have to open different files to find them. -->
</Page.Resources>
<ListView x:Name="MyListView">
<ListView.Resources>
<!-- Or place related resources here -->
</ListView.Resources>
</ListView>
</Page>
Hello I'm using Modern UI for WPF ,I have a page that contains a list with Items as links to other pages(User controls) my problem is when I press a link I have the error
System.IO.IO exception cannot locate resource 'basicpage1.xaml'
I have searched a lot but with no hope.
here is my XAML file for the list page:
<UserControl x:Class="ModernUINavigationApp.Pages.ListPage1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mui="http://firstfloorsoftware.com/ModernUI"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid Style="{StaticResource ContentRoot}">
<mui:ModernTab Layout="List" >
<mui:ModernTab.Links>
<!-- TODO: set #Source -->
<mui:Link DisplayName="Item 1" Source="/basicpage1.xaml"/>
<mui:Link DisplayName="Item 2" />
</mui:ModernTab.Links>
</mui:ModernTab>
</Grid>
</UserControl>
The Exception
Solution Explorer
basicpage1.xaml is located in your Pages directory so you should add /Pages to the source:
<mui:Link DisplayName="Item 1" Source="/Pages/basicpage1.xaml"/>
I just encountered the same kind of problem when setting a Frame Source to a page.
When checking file properties in solution explorer I noticed that VS has put the page file in a subdirectory of the application file (in my case VS named the subdirectory "My Project") so I rewrote
<Frame Source="/Page1.xaml"/>
to
<Frame Source="/My Project/Page1.xaml">
and it solved the problem.
Just check and compare the full path property of application.xaml versus your basicpage1.xaml"
I have this xaml
<mui:ModernWindow x:Uid="mui:ModernWindow_1" x:Class="App1.HomePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mui="http://firstfloorsoftware.com/ModernUI"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
ContentSource="/Window1.xaml"
Title="MainWindow" Height="350" Width="525" WindowState="Maximized" MenuLinkGroups="{Binding menuLinkGroups}">
<mui:ModernWindow.Resources>
<sys:String x:Key="ApplicationName">Bla Bla</sys:String>
</mui:ModernWindow.Resources>
<Grid Style="{StaticResource ContentRoot}" Name="mainGrid">
</Grid>
</mui:ModernWindow>
I need to reference current window resources, so I used this:
object obj = this.Resources["ApplicationName"];
But this.Resources doesn't have any resource! so obj is always null. How could I reference this window resources?
Assume that this is a FrameworkElement, like a Window, a Grid, a Button or something like that.
object obj = this.TryFindResource("ApplicationName");
Assuming this is a control...
var parent = Window.GetWindow(this)
Will get the window the control is currently on, you should then be able to access the resources like you already did
parent.Resources["ApplicationName"];
you can use below mentioned code
var MainGrid=Application.Current.FindResource("strApp")
or
this.FindResource("ApplicationName")
Thanks all, I find the solution (I forget to update the localized dll).
I cleaned and rebuilt solution, used locbaml.exe again to generate new localized dll.
You should bind in the XAML and not in the code behind.
"res" is the namespace where the resources file is located.
In your example the namespace alias is "local":
xmlns:local="clr-namespace:Project.Resources"
So your code should look like:
<Page Title ="{x:Static local:localStrings.step1Description}" />
Where:
"local" is the namespace alias where the resources file is located.
"localStrings" is the name of the resource file.
"step1Description" is an entry in the resource file.