Я хочу привязать два ярлыка к функциям моего кода.
Но эти две функции никогда не вызываются.
У меня есть следующая реализация: вызывается Fitst NavigationWindow, у него естьИсходное свойство следующим образом: Source="MainPage.xaml"
MainPage.xaml Код:
<Page x:Class="XXX.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:XXX"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="MainPage" DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}">
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<RoutedUICommand x:Key="Ctr1" Text="Another Text" />
<RoutedUICommand x:Key="Ctr2" Text="Another Text" />
</ResourceDictionary>
</Page.Resources>
<Page.InputBindings>
<KeyBinding Key="F10" Modifiers="Ctrl" Command="{StaticResource Ctr1}" />
<KeyBinding Key="F12" Modifiers="Ctrl" Command="{StaticResource Ctr2}" />
</Page.InputBindings>
<Page.CommandBindings>
<CommandBinding Command="{StaticResource Ctr1}" Executed="CtrShortcut1" />
<CommandBinding Command="{StaticResource Ctr2}" Executed="CtrShortcut2" />
</Page.CommandBindings>
<Grid/>
</Page>
MainPage.xaml.cs Код:
public MainPage()
{
InitializeComponent();
this.DataContext = this;
}
public void CtrShortcut1(Object sender, ExecutedRoutedEventArgs e)
{
FONCTIONS.ShowToast("success", "test");
}
public void CtrShortcut2(Object sender, ExecutedRoutedEventArgs e)
{
FONCTIONS.ShowToast("success", "test2");
}
Что я сделал не так?
Возможно ли даже связать ярлык на странице?
РЕДАКТИРОВАТЬ
xaml код:
<Page.InputBindings>
<KeyBinding Key="F12" Command="{Binding DoSomething}"/>
</Page.InputBindings>
xaml.cs код:
public ICommand DoSomething { get; set; }
private void doSomething(object obj)
{
FONCTIONS.ShowToast("success", "Shortcut work !");
}
public MainPage()
{
InitializeComponent();
this.DataContext = this;
DoSomething = new RelayCommand(doSomething);
}
РЕШЕНИЕ Я не могу найти способ вызова ярлыка из Controls.Page, но он работает с NavigationWindow и Window.Чтобы получить контекст страницы, я делаю ссылку на мою текущую страницу в статической переменной, затем вызываю StaticVariable.Foo ()
Код страницы:
public static dynamic CurrentPage;
public MainPage()
{
InitializeComponent();
this.DataContext = this;
FONCTIONS.CurrentPage = this;
}
И в моем коде MainWindow:
private void CurrentPageSetup1(object obj)
{
REF_TO_STATIC_CLASS.CurrentPage.OpenSetup();
}