WPF MVVM绑定窗口图标到视图模型中的属性

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF MVVM绑定窗口图标到视图模型中的属性相关的知识,希望对你有一定的参考价值。

我有一个WPF MVVM应用程序。在我的项目属性中,我在“应用程序”选项卡中设置了图标。

我试图统一从我的整个应用程序中的不同位置获取窗口图标的方式。所以我在类中创建了一个扩展方法:

internal static class IconUtilities
{
    [DllImport("gdi32.dll", SetLastError = true)]
    private static extern bool DeleteObject(IntPtr hObject);

    public static ImageSource ToImageSource(this Icon icon)
    {
        Bitmap bitmap = icon.ToBitmap();
        IntPtr hBitmap = bitmap.GetHbitmap();

        ImageSource wpfBitmap = Imaging.CreateBitmapSourceFromHBitmap(
            hBitmap,
            IntPtr.Zero,
            Int32Rect.Empty,
            BitmapSizeOptions.FromEmptyOptions());

        if (!DeleteObject(hBitmap))
        {
            throw new Win32Exception();
        }

        return wpfBitmap;
    }
}

在我的视图模型中有一个公共属性:

public Icon WindowIcon
{
    get
    {
        return Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location);
    }
}

public ImageSource GetWindowIcon()
{
    return WindowIcon.ToImageSource();
}

所以,无论我需要在视图模型中获取应用程序图标,我都会调用此属性来获取它。所以我确保我总是使用相同的应用程序图标。

现在,我试图通过执行以下操作将此属性绑定到窗口图标:

<Window x:Name="MainWindow" x:Class="My.Apps.WPF.wMain"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" 
    Icon="{Binding WindowIcon}"
/>
</Window>

但在运行时,显示的Window图标是默认值,而不是项目属性中设置的图标。我究竟做错了什么?

ATTEMPT#1:

这是在InitializeComponents之前调用的,所以我已经进入Window Loaded事件。

现在,来自Window:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    this.Icon = ((wMainViewModel)this.DataContext).WindowIcon;
}

我收到错误:

Cannot convert implicitly 'System.Drawing.Icon' type into 'System.Windows.Media.ImageSource'

所以我尝试在下面做:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    this.Icon = ((wMainViewModel)this.DataContext).GetWindowIcon();
}

现在我收到错误:

ImageSource for Icon property should be an icon file.

测试APP:


简单的WPF测试应用程序(不是MVVM) - 仅用于测试目的

下面我写了一个完整的WPF应用程序(不是MVVM),取自here并添加了一些代码。此示例代码与上述所有内容不同:此示例的目的是在将ImageSource分配给窗口图标时显示问题。它已在Visual Studio 2008中使用.NET Framework 3.5和Visual Studio 2015以及.NET Framework 3.5进行了测试。在设置this.Icon时,这两种情况都会在SplashScreen代码隐藏构造函数中失败,请参阅下面的代码。

用于测试的图标文件(* .ico)也是:“C: Program Files Internet Explorer images bing.ico”。此图标文件已添加到我的项目中,我已将构建操作设置为资源,而不是复制到输出目录作为其属性。

此外,在项目属性中,在应用程序选项卡中,我选择此图标(bing.ico)作为应用程序的图标。此图标将使用以下行从代码中提取:

System.Drawing.Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly()的位置。);

稍后在我的代码中,在构造函数中的SplashScreen代码隐藏中查看。

附加说明:我有Windows 8.1 Pro x64。

App.cs:

using System;
using System.Drawing;
using System.Reflection;
using System.Threading;
using System.Windows;

namespace SplashDemo
{
    class App: Application
    {
        [STAThread ( )]
        static void Main ( )
        { 
            Icon ico = Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location);
            Splasher.Splash = new SplashScreen(ico);
            Splasher.ShowSplash();

            for ( int i = 0; i < 5000; i++ )
            {                
                DispatcherHelper.DoEvents();
                Thread.Sleep ( 1 );
            }

            new App ( );
        }

        public App ( )
        {         
            StartupUri = new System.Uri ( "MainWindow.xaml", UriKind.Relative );

            Run ( );            
        }
    }
}

注意:Icon对象的类型为System.Drawing.Icon

DispatcherHelper类:

using System;
using System.Security.Permissions;
using System.Windows.Threading;

namespace SplashDemo
{
    public static class DispatcherHelper
    {
        /// <summary>
        /// Simulate Application.DoEvents function of <see cref=" System.Windows.Forms.Application"/> class.
        /// </summary>
        [SecurityPermissionAttribute ( SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode )]
        public static void DoEvents ( )
        {
            DispatcherFrame frame = new DispatcherFrame ( );
            Dispatcher.CurrentDispatcher.BeginInvoke ( DispatcherPriority.Background,
                new DispatcherOperationCallback ( ExitFrames ), frame );

            try
            {
                Dispatcher.PushFrame ( frame );
            }
            catch ( InvalidOperationException )
            {
            }
        }

        private static object ExitFrames ( object frame )
        {
            ( ( DispatcherFrame ) frame ).Continue = false;

            return null;
        }
    }
}

Splasher类:

using System;
using System.Windows;

namespace SplashDemo
{
    /// <summary>
    /// Helper to show or close given splash window
    /// </summary>
    public static class Splasher
    {
        /// <summary>
        /// 
        /// </summary>
        private static Window mSplash;

        /// <summary>
        /// Get or set the splash screen window
        /// </summary>
        public static Window Splash
        {
            get
            {
                return mSplash;
            }
            set
            {
                mSplash = value;
            }
        }

        /// <summary>
        /// Show splash screen
        /// </summary>
        public static void ShowSplash ( )
        {
            if ( mSplash != null )
            {
                mSplash.Show ( );
            }
        }
        /// <summary>
        /// Close splash screen
        /// </summary>
        public static void CloseSplash ( )
        {
            if ( mSplash != null )
            {
                mSplash.Close ( );

                if ( mSplash is IDisposable )
                    ( mSplash as IDisposable ).Dispose ( );
            }
        }
    }
}

SplashScreen类(代码隐藏):

using System.Drawing;
using System.Reflection;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace SplashDemo
{
    /// <summary>
    /// Interaction logic for SplashScreen.xaml
    /// </summary>
    public partial class SplashScreen : Window
    {
        public SplashScreen ()
        {
            InitializeComponent();
        }

        public SplashScreen(Icon icon) : this()
        { 
            // BELOW LINE CRASHES
            this.Icon =  System.Drawing.Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location);
        }
    }
}

关于SplashScreen类的注意事项:

  • 如果我使用this.Icon = System.Drawing.Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly()。Location);它在编译时抛出错误:

不能隐式地将类型'system.drawing.icon'转换为'system.windows.media.imagesource'

  • 如果我用这个替换这一行:this.Icon = icon.ToImageSource2();

然后它抛出一个运行时错误(不是编译错误),一个System.InvalidOperationException:

Icon属性的ImageSource必须是图标文件

SplashScreen视图:

<Window 
    x:Class="SplashDemo.SplashScreen"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:SplashDemo" 

    Title="SplashScreen" Height="236" Width="414" WindowStartupLocation="CenterScreen" WindowStyle="None" 
    Background="Orange" BorderBrush="DarkOrange" BorderThickness="3" 
    ShowInTaskbar="False" ResizeMode="NoResize">

    <Grid>
        <Grid.RowDefinitions>            
            <RowDefinition Height="2*" />
            <RowDefinition Height="0.5*" />
        </Grid.RowDefinitions>

        <Label Grid.Row="0" Name="label1" FontSize="48" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Foreground="MintCream">
            <Label.BitmapEffect>
                <OuterGlowBitmapEffect GlowSize="15" />
            </Label.BitmapEffect> Splash screen
        </Label>

        <Label Grid.Row="1" x:Name="CurrentTask" 
               FontFamily="Microsoft Sans Serif" FontSize="18" FontStyle="Italic"  
               Foreground="White" Content="Loading">
            <Label.Style>
                <Style TargetType="Label">
                    <Style.Triggers>
                        <EventTrigger RoutedEvent="Label.Loaded">
                            <EventTrigger.Actions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Content" Duration="00:00:00.8" RepeatBehavior="Forever">
                                            <DiscreteObjectKeyFrame KeyTime="00:00:00.0" Value="Loading"/>
                                            <DiscreteObjectKeyFrame KeyTime="00:00:00.2" Value="Loading."/>
                                            <DiscreteObjectKeyFrame KeyTime="00:00:00.4" Value="Loading.."/>
                                            <DiscreteObjectKeyFrame KeyTime="00:00:00.6" Value="Loading..."/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </BeginStoryboard>
                            </EventTrigger.Actions>
                        </EventTrigger>
                    </Style.Triggers>
                </Style>
            </Label.Style>
        </Label>

    </Grid>
</Window>

MainWindow查看:

<Window 
    x:Class="SplashDemo.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Main window" Height="332" Width="539" WindowStartupLocation="CenterScreen">
    <Grid>
        <Label Margin="39,59,21,102" Name="label1" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" FontSize="36">Application window</Label>
    </Grid>
</Window>

MainWindow Code-Behind:

using System.Windows;

namespace SplashDemo
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow ( )
        {
            InitializeComponent ( );
        }
    }
}

IconUtilities类(从System.Drawing.Icon转换为System.Windows.Media.ImageSource):

using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace SplashDemo
{
    internal static class IconUtilities
    {
        [DllImport("gdi32.dll", SetLastError = true)]
        private static extern bool DeleteObject(IntPtr hObject);

        public static ImageSource ToImageSource(this Icon icon)
        {
            Bitmap bitmap = icon.ToBitmap();
            IntPtr hBitmap = bitmap.GetHbitmap();

            ImageSource wpfBitmap = Imaging.CreateBitmapSourceFromHBitmap(
                hBitmap,
                IntPtr.Zero,
                Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());

            if (!DeleteObject(hBitmap))
            {
                throw new Win32Exception();
            }

            return wpfBitmap;
        }

        // Below a simple method without extra objects:
        public static ImageSource ToImageSource2(this Icon icon)
        {
            ImageSource imageSource = Imaging.CreateBitmapSourceFromHIcon(
                                          icon.Handle,
                                          Int32Rect.Empty,
                                          BitmapSizeOptions.FromEmptyOptions());

            return imageSource;
        }
    }
}

注意:

  • ImageSource的类型为System.Windows.Media.ImageSource
  • ToImageSource2与ToImageSource的作用相同,但没有额外的对象,请参阅here
答案

绑定

<Window ... Icon="{Binding WindowIcon}">

要求WindowIcon属性是ImageSource

所以你当然想要实现你的视图模型,如下所示,它在Loaded事件处理程序中没有任何代码。

public ImageSource WindowIcon
{
    get { return GetWindowIcon().ToImageSource(); }
}

public static Icon GetWindowIcon()
{
    return Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location);
}

以上是关于WPF MVVM绑定窗口图标到视图模型中的属性的主要内容,如果未能解决你的问题,请参考以下文章

WPF MVVM 文本框文本绑定与 changedText 事件

使用 MVVM 和视图模型通信的 WPF 窗口模式对话框

WPF(MVVM):从 Viewmodel 关闭视图?

WPF 的综合指南:MVVM 与 MVP

窗口加载时WPF级联组合框不绑定

如何从作为wpf mvvm模式中的窗口打开的视图模型中关闭用户控件?