WPF:从文件加载资源目录抛出异常'初始化System.Windows.Setter'

时间:2014-04-18 01:13:02

标签: c# wpf

我在WPF中创建了一个可换肤的步步高应用程序,只要我在项目中包含外观文件(=资源目录)并在程序集中编译它就可以正常工作。但是,当我从项目中删除皮肤文件并尝试在运行时从文件加载它时,我收到以下错误:'初始化' System.Windows.Setter'提出异常'。

这是加载项目中包含的资源目录(工作正常)的代码:

private void SetSkin(string SkinName)
    {
        Collection<ResourceDictionary> appResources;
        appResources = App.Current.Resources.MergedDictionaries;
        ResourceDictionary skin = new ResourceDictionary();
        try
        {
            Uri skinUri = new Uri(SkinName + ".xaml", UriKind.Relative);
            skin.Source = skinUri;
        }
        catch (Exception e)
        {
            MessageBox.Show("Error Loading the skin " + SkinName + ":\n" + e.Message, "Unable to load skin", MessageBoxButton.OK, MessageBoxImage.Error);
        }
        appResources.Add(skin);
        if (_CurrentSkin != null) appResources.Remove(_CurrentSkin);
        _CurrentSkin = skin;
    }

当我将Uri更改为下面的代码以便在运行时加载皮肤时,我收到错误:&#39;&#39; System.Windows.Setter&#39;提出异常&#39;

Uri skinUri = new Uri(Directory.GetCurrentDirectory() + "\\Skins\\" + SkinName + "\\" + SkinName + ".xaml", UriKind.RelativeOrAbsolute);

错误发生在皮肤文件中的以下setter:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:BackgammonView">

<Style x:Key="MainGrid" TargetType="Grid">
    <Setter Property="local:BgBoardView.GridRows" Value="44*,45*,45*,22*,32*,10*,42*,10*,32*,22*,45*,45*,43*"/>
</Style>

上面的字符串通过附加属性设置主网格的行定义&#39; GridRows&#39; BgBoardView类(这是窗口)。

这是窗口的XAML和主网格:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:System="clr-namespace:System;assembly=mscorlib"
    xmlns:Properties="clr-namespace:BackgammonView.Properties"
    x:Class="BackgammonView.BgBoardView" x:Name="MainWindow"
    Icon="SkinBackgammon.ico" ResizeMode="CanResizeWithGrip"
    Style="{DynamicResource MainWindow}" Closing="MainWindow_Closing">

<Grid x:Name="MainGrid" Style="{DynamicResource MainGrid}">

只要我在项目中包含并编译资源目录,代码就可以正常工作。这是设置网格行定义的代码:

namespace BackgammonView
{
    public partial class BgBoardView : Window
    {
        private static DependencyProperty GridRowsProperty = DependencyProperty.RegisterAttached("GridRows", typeof(string),
          typeof(BgBoardView), new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.AffectsArrange,
          new PropertyChangedCallback(GridRowsChanged)));
        public static string GetGridRows(Grid Grid) { return Convert.ToString(Grid.GetValue(GridRowsProperty)); }
        public static void SetGridRows(Grid Grid, string Value) { Grid.SetValue(GridRowsProperty, Value); }
        private static void GridRowsChanged(Object Sender, DependencyPropertyChangedEventArgs e)
        {
            Grid Grid = (Grid)Sender;
            string[] sGridLength = GetGridRows(Grid).Split(',').Select(sValue => sValue.Trim()).ToArray();
            GridLengthConverter convert = new GridLengthConverter();
            Grid.RowDefinitions.Clear();
            for (int i = 0; i < sGridLength.Length; i++)
            {
                GridLength GridLength = (GridLength)convert.ConvertFromString(sGridLength[i]);
                RowDefinition rowdef = new RowDefinition();
                rowdef.Height = GridLength;
                Grid.RowDefinitions.Add(rowdef);
            }
        }
    }
}

有没有人知道为什么当我在运行时动态加载资源目录时,它不会起作用?

非常感谢任何帮助。 TX。

1 个答案:

答案 0 :(得分:1)

尝试在xmlns:local定义中包含程序集。当您从已编译的程序集中加载它时,该部分将从包含的程序集中为您解析,但在作为松散文件加载时,它没有要使用的包含程序集。