I have defined a resource in App.xaml and assign it a key. I get System.Windows.Markup.XamlParseException
exception when I reference the resource from a AddUser.xaml
window in the app. The error occurs at line InitializeComponent();
(of AddUser.xamlcs
file) that is called when I instantiate the AddUser
object inside btnNewUser_Click(..)
of Mainwindow.xaml.cs
.
According to this response on a similar issue, the easiest way is to place the resource into App.xaml (that is what I'm actually doing). Question: What I may be missing here and how the issue can be resolved?
Remarks: As done here as well, you may notice that since I'm using Commandds
, I'm using the namespace
WpfApp1.Commands
in App.xaml and App.xam.cs files.
Error:
System.Windows.Markup.XamlParseException
HResult=0x80131501
Message='Provide value on 'System.Windows.StaticResourceExtension' threw an exception.
.....
Inner Exception 1:
Exception: Cannot find resource named 'Auser'. Resource names are case sensitive.
App.Xaml:
<Application x:Class="WpfApp1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp1.Commands"
StartupUri="MainWindow.xaml">
<Application.Resources>
<local:AddUser x:Key="Auser"/>
</Application.Resources>
</Application>
MainWindow.xaml:
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Button x:Name="btnNewUser" Content="Create User" Click="btnNewUser_Click"/>
</Grid>
</Window>
MainWindow.xaml.cs:
using System.Windows;
using WpfApp1.Commands;
namespace WpfApp1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnNewUser_Click(object sender, RoutedEventArgs e)
{
AddUser addUser = new AddUser();
if(addUser.ShowDialog()==true)
MyAddUser(addUser.UserName, addUser.Department);
}
private void MyAddUser(string sUserName, string sDepartment)
{
//will add code here
}
}
}
AddUser.xaml:
<Window x:Class="WpfApp1.Commands.AddUser"
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:WpfApp1"
mc:Ignorable="d"
Title="AddUser" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Content="User Name:"/>
<TextBlock x:Name="txtUserName" Grid.Column="1">
<TextBlock.Text>
<Binding Path="UserName" Source="{StaticResource Auser}"/>
</TextBlock.Text>
</TextBlock>
<Label Content="Department:" Grid.Row="1"/>
<TextBox x:Name="txtDepartment" Grid.Row="1" Grid.Column="1">
<TextBox.Text>
<Binding Path="Department" Source="{StaticResource Auser}"/>
</TextBox.Text>
</TextBox>
<Button x:Name="btnAdd" Content="Add User" Grid.Row="2" Grid.Column="1">
<Button.CommandBindings>
<CommandBinding Command="New" CanExecute="CommandNew_CanExecute" Executed="CommandNew_Executed"/>
</Button.CommandBindings>
</Button>
</Grid>
</Window>
AddUser.xaml.cs:
NOTE: Error occurs at the line InitializeComponent();
when AddUser object is instantiated inside btnNewUser_Click(..)
of Mainwindow.xaml.cs
using System.Windows;
using System.Windows.Input;
namespace WpfApp1.Commands
{
public partial class AddUser : Window
{
public AddUser()
{
InitializeComponent();
}
public string UserName
{
get { return txtUserName.Text; }
}
public string Department
{
get { return txtDepartment.Text; }
}
private void CommandNew_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
}
private void CommandNew_Executed(object sender, ExecutedRoutedEventArgs e)
{
}
}
}
User contributions licensed under CC BY-SA 3.0