csharpc-sharpdotnetxamlavaloniauicross-platformcross-platform-xamlavaloniaguimulti-platformuser-interfacedotnetcore
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
99 lines
3.5 KiB
99 lines
3.5 KiB
using System.Reactive.Subjects;
|
|
using System.Windows.Input;
|
|
using Avalonia.Controls;
|
|
using Avalonia.Input;
|
|
using Avalonia.UnitTests;
|
|
using Xunit;
|
|
|
|
namespace Avalonia.Markup.Xaml.UnitTests.Data
|
|
{
|
|
public class BindingTests_Method : XamlTestBase
|
|
{
|
|
[Fact]
|
|
public void Binding_Method_To_Command_Works()
|
|
{
|
|
using (UnitTestApplication.Start(TestServices.StyledWindow))
|
|
{
|
|
var xaml = @"
|
|
<Window xmlns='https://github.com/avaloniaui'
|
|
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
|
|
xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'>
|
|
<Button Name='button' Command='{Binding Method}'/>
|
|
</Window>";
|
|
var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
|
|
var button = window.FindControl<Button>("button");
|
|
var vm = new ViewModel();
|
|
|
|
button.DataContext = vm;
|
|
window.ApplyTemplate();
|
|
|
|
Assert.NotNull(button.Command);
|
|
PerformClick(button);
|
|
Assert.Equal("Called", vm.Value);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void Binding_Method_With_Parameter_To_Command_Works()
|
|
{
|
|
using (UnitTestApplication.Start(TestServices.StyledWindow))
|
|
{
|
|
var xaml = @"
|
|
<Window xmlns='https://github.com/avaloniaui'
|
|
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
|
|
xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'>
|
|
<Button Name='button' Command='{Binding Method1}' CommandParameter='5'/>
|
|
</Window>";
|
|
var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
|
|
var button = window.FindControl<Button>("button");
|
|
var vm = new ViewModel();
|
|
|
|
button.DataContext = vm;
|
|
window.ApplyTemplate();
|
|
|
|
Assert.NotNull(button.Command);
|
|
PerformClick(button);
|
|
Assert.Equal("Called 5", vm.Value);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void Binding_Method_To_TextBlock_Text_Works()
|
|
{
|
|
using (UnitTestApplication.Start(TestServices.StyledWindow))
|
|
{
|
|
var xaml = @"
|
|
<Window xmlns='https://github.com/avaloniaui'
|
|
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
|
|
xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'>
|
|
<TextBlock Name='textBlock' Text='{Binding Method}'/>
|
|
</Window>";
|
|
var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
|
|
var textBlock = window.FindControl<TextBlock>("textBlock");
|
|
var vm = new ViewModel();
|
|
|
|
textBlock.DataContext = vm;
|
|
window.ApplyTemplate();
|
|
|
|
Assert.NotNull(textBlock.Text);
|
|
}
|
|
}
|
|
|
|
static void PerformClick(Button button)
|
|
{
|
|
button.RaiseEvent(new KeyEventArgs
|
|
{
|
|
RoutedEvent = InputElement.KeyDownEvent,
|
|
Key = Input.Key.Enter,
|
|
});
|
|
}
|
|
|
|
private class ViewModel
|
|
{
|
|
public string Method() => Value = "Called";
|
|
public string Method1(int i) => Value = $"Called {i}";
|
|
public string Method2(int i, int j) => Value = $"Called {i},{j}";
|
|
public string Value { get; private set; } = "Not called";
|
|
}
|
|
}
|
|
}
|
|
|