相关疑难解决方法(0)

无法引用包含合并字典的资源字典

我有一个库,CommonLibraryWpfThemes,里面有几个Resource Dictionary XAML文件.My Themes/Generic.xml文件包含一个ResourceDictionary.MergedDictionaries声明,它将所有其他文件合并在一起.

Generic.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary
            Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/BrushDictionary.xaml" />
        <ResourceDictionary
            Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/TextBlockDictionary.xaml" />
        <ResourceDictionary
            Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/LabelDictionary.xaml" />
        <ResourceDictionary
            Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/ButtonDictionary.xaml" />
        <ResourceDictionary
            Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/WindowDictionary.xaml" />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

在我的应用程序项目中,我引用了CommonLibraryWpfThemes,并在我的App.xaml文件中显式引用了Generic.xml.

App.xaml - 失败

<Application
    x:Class="MyApp.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Application.Resources>
        <ResourceDictionary
            Source="/CommonLibraryWpfThemes;component/Themes/Generic.xaml" />
    </Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)

这不起作用.运行我的应用程序时出现以下错误:

System.Windows.Markup.XamlParseException occurred
  Message="Cannot find resource named '{_fadedOrangeBrush}'. Resource names are case sensitive.  Error at object 'System.Windows.Setter' in markup file 'CommonLibraryWpfThemes;component/ResourceDictionaries/WindowDictionary.xaml' Line 18 Position 13."
  Source="PresentationFramework"
  LineNumber=18
  LinePosition=13
Run Code Online (Sandbox Code Playgroud)

如果我直接将Generic.xaml的内容放入App.xaml,一切正常:

App.xaml - SUCCEEDS

<Application
    x:Class="MyApp.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary …
Run Code Online (Sandbox Code Playgroud)

wpf xaml themes resourcedictionary

44
推荐指数
3
解决办法
4万
查看次数

在MVVM WPF中打开新窗口

我有Button,我已将此按钮绑定到ViewModel中的命令说OpenWindowCommand.当我点击按钮时,我想打开新窗口.但是创建窗口实例并从视图模型显示窗口违反了MVVM.我已经创建了界面

interface IWindowService
{
 void showWindow(object dataContext);
}
Run Code Online (Sandbox Code Playgroud)

和WindowService一样实现这个接口

class WindowService:IWindowService
{
 public void showWindow(object dataContext)
 {
  ChildWindow window=new ChildWindow();
  window.DataContext=dataContext;
  window.Show();
  }
}
Run Code Online (Sandbox Code Playgroud)

在这个课程中,我指定了ChildWindow.因此,这个类与显示ChildWindow紧密结合.当我想要显示另一个窗口时,我必须实现具有相同接口和逻辑的另一个类.如何使这个类通用,以便我可以传递任何窗口的实例,并且类将能够打开任何窗口?我没有使用任何构建的MVVM框架.我已经阅读了很多关于StackOverflow的文章,但我找不到任何解决方案.

c# wpf mvvm

39
推荐指数
2
解决办法
6万
查看次数

你如何使用Application.Resources中定义的DataTemplate?

如果我的Windows无法访问其中定义的资源,Application.Resources的目的是什么?

这有效,我得到一个带有TextBox的窗口,里面写着"Loki"......

App.xaml.cs:

public partial class App : Application
{
  protected override void OnStartup(StartupEventArgs e)
  {
    base.OnStartup(e);

    ViewModel.ViewModel1 oVM = new ViewModel.ViewModel1 { Name = "Loki" };
    MainWindow oVW = new MainWindow { Content = oVM };

    oVW.ShowDialog();
  }
}
Run Code Online (Sandbox Code Playgroud)

MainWindow.xaml

<Window x:Class="TableGenerator.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="clr-namespace:TableGenerator.ViewModel"
        Title="MainWindow" Height="350" Width="525">
  <Window.Resources>
    <DataTemplate DataType="{x:Type vm:ViewModel1}">
      <TextBox Text="{Binding Path=Name}" />
    </DataTemplate>
  </Window.Resources>
  <ContentPresenter />
</Window>
Run Code Online (Sandbox Code Playgroud)

但是将DataTemplate移动到Application.Resources而不是Window.Resources不起作用.当我运行这个时,我得到一个窗口,根本没有TextBox,但是有一些文本显示只是说我的viewmodel类的名称,"TableGenerator.ViewModel.ViewModel1".

App.xaml.cs没有变化.

MainWindow.xaml更改为:

<Window x:Class="TableGenerator.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
  <ContentPresenter />
</Window>
Run Code Online (Sandbox Code Playgroud)

App.xaml中:

<Application x:Class="TableGenerator.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" …
Run Code Online (Sandbox Code Playgroud)

c# wpf xaml mvvm

2
推荐指数
1
解决办法
3196
查看次数

标签 统计

wpf ×3

c# ×2

mvvm ×2

xaml ×2

resourcedictionary ×1

themes ×1