-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[XC] Allow generic types in x:DataType and x:Type (#20625)
* Add parser for single type expression * Allow generic types in x:DataType * Allow generic types in x:Type * Add test * Improve test
- Loading branch information
1 parent
eac12cd
commit 76e9f74
Showing
6 changed files
with
119 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
xmlns:local="clr-namespace:Microsoft.Maui.Controls.Xaml.UnitTests" | ||
x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.Maui20616"> | ||
<ContentPage.Resources> | ||
<x:Type x:Key="ViewModelBool" TypeName="local:ViewModel20616(x:Boolean)" /> | ||
<x:Type x:Key="NestedViewModel" TypeName="local:ViewModel20616(local:ViewModel20616(x:String))" /> | ||
</ContentPage.Resources> | ||
|
||
<Label Text="{Binding Value}" x:DataType="local:ViewModel20616(x:String)" x:Name="LabelA" /> | ||
<Label Text="{Binding Value.Value}" x:DataType="{x:Type local:ViewModel20616(local:ViewModel20616(x:Boolean))}" x:Name="LabelB" /> | ||
</ContentPage> |
76 changes: 76 additions & 0 deletions
76
src/Controls/tests/Xaml.UnitTests/Issues/Maui20616.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Globalization; | ||
using System.Linq; | ||
using Microsoft.Maui.ApplicationModel; | ||
using Microsoft.Maui.Controls.Core.UnitTests; | ||
using Microsoft.Maui.Controls.Shapes; | ||
using Microsoft.Maui.Controls.Internals; | ||
using Microsoft.Maui.Devices; | ||
using Microsoft.Maui.Dispatching; | ||
|
||
using Microsoft.Maui.Graphics; | ||
using Microsoft.Maui.UnitTests; | ||
using NUnit.Framework; | ||
|
||
namespace Microsoft.Maui.Controls.Xaml.UnitTests; | ||
|
||
public partial class Maui20616 | ||
{ | ||
public Maui20616() | ||
{ | ||
InitializeComponent(); | ||
BindingContext = new ViewModel20616<string> { Value = "Foo" }; | ||
} | ||
|
||
public Maui20616(bool useCompiledXaml) | ||
{ | ||
//this stub will be replaced at compile time | ||
} | ||
|
||
[TestFixture] | ||
class Test | ||
{ | ||
[SetUp] | ||
public void Setup() | ||
{ | ||
Application.SetCurrentApplication(new MockApplication()); | ||
DispatcherProvider.SetCurrent(new DispatcherProviderStub()); | ||
} | ||
|
||
[TearDown] public void TearDown() => AppInfo.SetCurrent(null); | ||
|
||
[Test] | ||
public void XDataTypeCanBeGeneric([Values(false, true)] bool useCompiledXaml) | ||
{ | ||
var page = new Maui20616(useCompiledXaml); | ||
|
||
page.LabelA.BindingContext = new ViewModel20616<string> { Value = "ABC" }; | ||
Assert.AreEqual("ABC", page.LabelA.Text); | ||
|
||
if (useCompiledXaml) | ||
{ | ||
var binding = page.LabelA.GetContext(Label.TextProperty).Bindings.Values.Single(); | ||
Assert.That(binding, Is.TypeOf<TypedBinding<ViewModel20616<string>, string>>()); | ||
} | ||
|
||
page.LabelB.BindingContext = new ViewModel20616<ViewModel20616<bool>> { Value = new ViewModel20616<bool> { Value = true } }; | ||
Assert.AreEqual("True", page.LabelB.Text); | ||
|
||
if (useCompiledXaml) | ||
{ | ||
var binding = page.LabelB.GetContext(Label.TextProperty).Bindings.Values.Single(); | ||
Assert.That(binding, Is.TypeOf<TypedBinding<ViewModel20616<ViewModel20616<bool>>, bool>>()); | ||
} | ||
|
||
Assert.AreEqual(typeof(ViewModel20616<bool>), page.Resources["ViewModelBool"]); | ||
Assert.AreEqual(typeof(ViewModel20616<ViewModel20616<string>>), page.Resources["NestedViewModel"]); | ||
} | ||
} | ||
} | ||
|
||
public class ViewModel20616<T> | ||
{ | ||
public required T Value { get; init; } | ||
} |