1

アプリの起動時に Application.Current.Resources に追加される ResourceDictionary で定義されたNotifyIconを持つ WPF アプリケーションがあります。

私は MVVM-Light フレームワークを使用しており、NotifyIcon の ContextMenu.MenuItems の Command プロパティを、ViewModel で定義された public RelayCommand にバインドしたいと考えています。

View を ViewModel に結合することには慣れていますが、グローバル リソースを ViewModel に結合するにはどうすればよいですか?

これを機能させるための私の試みは次のとおりです。正しい行にいるかどうかはわかりません...

このコードを実行すると、「'Locator' という名前のリソースが見つかりません。リソース名は大文字と小文字が区別されます。」というエラーが表示されます。これは、NotificationIconResources.xaml の TaskBarIcon タグの DataContext バインディングに由来します。

SingleInstanceManager により、インスタンスを 1 つだけ作成できるようになります

    public sealed class SingleInstanceManager : WindowsFormsApplicationBase
{
    [STAThread]
    public static void Main(string[] args)
    {
        (new SingleInstanceManager()).Run(args);
    }

    public SingleInstanceManager()
    {
        IsSingleInstance = true;
    }

    public ControllerApp App { get; private set; }

    protected override bool OnStartup(StartupEventArgs e)
    {
        App = new ControllerApp();
        App.Run();
        return false;
    }

    protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
    {
        base.OnStartupNextInstance(eventArgs);
        App.MainWindow.Activate();
        App.ProcessArgs(eventArgs.CommandLine.ToArray(), false);
    }
}

ControllerApp は App.xaml と App.xaml.cs を置き換えます

public class ControllerApp : Application
{
    public MainWindow window { get; private set; }
    bool startMinimized = false;
    private TaskbarIcon tb;

    public ControllerApp()
        : base()
    { }

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        DispatcherHelper.Initialize();

        ResourceDictionary dict = new ResourceDictionary();
        dict.Source = new Uri("NotificationIconResources.xaml", UriKind.Relative);
        Application.Current.Resources.MergedDictionaries.Add(dict);

        ViewModel.ViewModelLocator vmLocator = new ViewModel.ViewModelLocator();
        Application.Current.Resources.Add("Locator", vmLocator);

        window = new MainWindow();
        ProcessArgs(e.Args, true);

        //initialize NotifyIcon
        tb = (TaskbarIcon)FindResource("ItemNotifyIcon");

        if (startMinimized)
        {
            window.WindowState = WindowState.Minimized;
        }

        window.Show();
    }

    protected override void OnExit(ExitEventArgs e)
    {
        base.OnExit(e);

        tb.Dispose();
    }

    public void ProcessArgs(string[] args, bool firstInstance)
    {

    }
}

NotificationIconResources.xaml は、NotifyIcon を定義するリソース ディクショナリです。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:tb="http://www.hardcodet.net/taskbar">

    <tb:TaskbarIcon x:Key="ItemNotifyIcon"
                    IconSource="/Controller;component/Images/ItemRunning.ico"
                    IsNoWaitForDoubleClick="True"
                    ToolTipText="Item is running" 
                    DataContext="{Binding NotifyIcon, Source={StaticResource Locator}}">

        <tb:TaskbarIcon.ContextMenu>
            <ContextMenu>
                <MenuItem Header="Open Control Panel" />
                <Separator />
                <MenuItem Header="Start Item" Command="{Binding Path=StartServiceCommand}" />
                <MenuItem Header="Pause Item" />
                <MenuItem Header="Stop Item" Command="{Binding Path=StopServiceCommand}" />
                <Separator />
                <MenuItem Header="Close" />
            </ContextMenu>
        </tb:TaskbarIcon.ContextMenu>

    </tb:TaskbarIcon>
</ResourceDictionary>

NotifyIconViewModel には、バインドしたい RelayCommands が含まれています

    /// <summary>
/// This class contains properties that the NotifyIcon View can data bind to.
/// <para>
/// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
/// </para>
/// <para>
/// You can also use Blend to data bind with the tool's support.
/// </para>
/// <para>
/// See http://www.galasoft.ch/mvvm/getstarted
/// </para>
/// </summary>
public class NotifyIconViewModel : ViewModelBase
{
    private ServiceController sc;

    public string Welcome
    {
        get
        {
            return "Welcome to MVVM Light";
        }
    }

    /// <summary>
    /// Initializes a new instance of the NotifyIconViewModel class.
    /// </summary>
    public NotifyIconViewModel()
    {
        if (IsInDesignMode)
        {
            // Code runs in Blend --> create design time data.
        }
        else
        {
            sc = new ServiceController("Item");
        }
    }

    #region Public Commands

    private RelayCommand _startServiceCommand = null;

    public RelayCommand StartServiceCommand
    {
        get
        {
            if (_startServiceCommand == null)
            {
                _startServiceCommand = new RelayCommand(
                    () => this.OnStartServiceCommand(),
                    () => (sc.Status == ServiceControllerStatus.Stopped));
            }
            return _stopServiceCommand;
        }
    }

    private void OnStartServiceCommand()
    {
        try
        {
            sc.Start();
        }
        catch (Exception ex)
        {
            // notify user if there is any error
            AppMessages.RaiseErrorMessage.Send(ex);
        }
    }

    private RelayCommand _stopServiceCommand = null;

    public RelayCommand StopServiceCommand
    {
        get
        {
            if (_stopServiceCommand == null)
            {
                _stopServiceCommand = new RelayCommand(
                    () => this.OnStopServiceCommand(),
                    () => (sc.CanStop && sc.Status == ServiceControllerStatus.Running));
            }
            return _stopServiceCommand;
        }
    }

    private void OnStopServiceCommand()
    {
        try
        {
            sc.Stop();
        }
        catch (Exception ex)
        {
            // notify user if there is any error
            AppMessages.RaiseErrorMessage.Send(ex);
        }
    }

    #endregion

    ////public override void Cleanup()
    ////{
    ////    // Clean up if needed

    ////    base.Cleanup();
    ////}
}
4

2 に答える 2

0

助けてくれてありがとうジョン。

私は問題を解決しました。

ControllerApp.cs から次の行を削除しました

    ViewModel.ViewModelLocator vmLocator = new ViewModel.ViewModelLocator();
    Application.Current.Resources.Add("Locator", vmLocator);

ViewModelLocator を ResourceDictionary (NotificationIconResources.xaml) に次のように追加しました。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:tb="http://www.hardcodet.net/taskbar"
                xmlns:vm="clr-namespace:Controller.ViewModel">

<vm:ViewModelLocator x:Key="Locator"/>


    <tb:TaskbarIcon x:Key="ItemNotifyIcon"
                    IconSource="/Controller;component/Images/ItemRunning.ico"
                    IsNoWaitForDoubleClick="True"
                    ToolTipText="Item is running" 
                    DataContext="{Binding NotifyIcon, Source={StaticResource Locator}}">

        <tb:TaskbarIcon.ContextMenu>
            <ContextMenu>
                <MenuItem Header="{Binding Path=Item}" />
                <Separator />
                <MenuItem Header="Start Item" Command="{Binding Path=StartServiceCommand}" />
                <MenuItem Header="Pause Item" />
                <MenuItem Header="Stop Item" Command="{Binding Path=StopServiceCommand}" />
                <Separator />
                <MenuItem Header="Close" />
            </ContextMenu>
        </tb:TaskbarIcon.ContextMenu>

    </tb:TaskbarIcon>

</ResourceDictionary>
于 2011-03-31T13:54:08.840 に答える
0

アプリケーション レベルで NotifyIcon を宣言した場合、どのビューのビジュアル ツリーにもないため、別のビューの ViewModel を継承させることはできません。あなたの最善の策は、NotifyIcon にコマンドが定義された独自の ViewModel を提供し、UI 間ではなく ViewModel 間の通信を処理することです。

特定のビューの ViewModel にバインドする必要がある場合は、グローバルではなくそのビューで宣言することも検討できます。その場合、使用しようとしている DataContext を自動的に継承できます (ただし、そのビューで開いたり閉じたりします) )。

于 2011-03-31T12:33:00.590 に答える