`

WPF 后台动态调用样式文件,就像CSS样式表文件一样

wpf 
阅读更多
应用场合:如果您的WPF应用程序设置WPF运行一个实例代码后,App.xaml文件中对样式资源字典文件的引用将失效.

解决办法1:在App.xaml.cs文件中用反射动态调用另外一个DLL项目中的样式文件即可

解决办法2:在每个窗体的xaml文件中添加对指定样式文件的引用(本文不做介绍)

详细操作介绍如下:

1、WPF设置只运行一个实例代码:

App.xaml文件代码如下:
    <Application.Resources>         
     <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
         <ResourceDictionary Source="ButtonStyle.xaml"/>
     </ResourceDictionary>        
    </Application.Resources>
</Application>

App.xaml.cs文件代码如下:

//添加引用
using System.Diagnostics;
using System.IO;
using System.Reflection;

namespace WpfUI
{

    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {

        public App()
        {
        }

        /// <summary>
        /// 要设置App.xaml的文件属性中生成操作=无
        /// </summary>
        [STAThread]       
        public static void Main()
        {
            App myApp = new App();                         
            myApp.ShutdownMode = ShutdownMode.OnExplicitShutdown;
            myApp.Run();
        }

        private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            if (e.Exception is InvalidOperationException)
                e.Handled = true;
        }

        protected override void OnStartup(StartupEventArgs e)
        {
            //获取当前运行WPF程序的进程实例
            Process process = Process.GetCurrentProcess();
            //遍历WPF程序的同名进程组
            foreach (Process p in Process.GetProcessesByName(process.ProcessName))
            {
                if (p.Id != process.Id && (p.StartTime - process.StartTime).TotalMilliseconds <= 0)
                {
                    p.Kill();//关闭进程
                    return;
                }
            }
            base.OnStartup(e);
            //启动登陆窗体,

          TestWindow myWindow = new TestWindow();

          myWindow.Show();
      }

    }

}

2、ButtonStyle.xaml样式文件内容如下:

<ResourceDictionary
    xmlns=" http://schemas.microsoft.com/winfx/2006/xaml/presentation "
    xmlns:x=" http://schemas.microsoft.com/winfx/2006/xaml "
    xmlns:ed=" http://schemas.microsoft.com/expression/2010/drawing ">

    <!--按钮样式-->
    <Style x:Key="RedButtonStyle" TargetType="{x:Type Button}">
        <Setter Property="Foreground" Value="Red"/>
        <Setter Property="Background" Value="Silver"/>
        <Setter Property="Height" Value="23"/>
    </Style>
</ResourceDictionary>

3、TestWindow.xaml文件内容如下:

<Grid>

<Button x:Name="MyButton" Content="测试按钮" Style="{StaticResource RedButtonStyle}" Margin="144.988,8,0,0"  HorizontalAlignment="Left" Width="57"  FontSize="16" VerticalAlignment="Top" Height="24.687"/>

</Grid>

4、运行程序后发现按钮样式RedButtonStyle总提示找不到

5、 解决办法如下:

第一步: 新建一个Windows--类库项目WpfThems,将ButtonStyle.xaml拷贝过去,生成WpfThems.dll文件

第二步:在当前项目WpfUI中添加WpfThems项目引用

第三步:修改App.xaml.cs 文件代码:

添加一个函数
        private void LoadStyleResource()
        {

                   Assembly assembly = Assembly.LoadFrom("WpfThems.dll");
                   string packUri = @"/WpfThems;component/ButtonStyle.xaml");
                    ResourceDictionary myResourceDictionary = Application.LoadComponent(new Uri(packUri, UriKind.Relative)) as ResourceDictionary;
                    this.Resources.MergedDictionaries.Add(myResourceDictionary);

          }

在OnStartup函数中添加上面样式加载函数的调用即可

        protected override void OnStartup(StartupEventArgs e)
        {

          LoadAllStyleResource():

          //后面的代码省略,请参考上面代码

        }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics