-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[XC] Produce warning when x:DataType is inherited from outer scope of DataTemplate
#22803
Merged
simonrozsival
merged 4 commits into
dotnet:net9.0
from
simonrozsival:xamlc-warning-for-xdatatype-from-outer-scope
Jun 21, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,28 @@ | ||
<?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" | ||
x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.Maui22714" | ||
xmlns:local="clr-namespace:Microsoft.Maui.Controls.Xaml.UnitTests"> | ||
|
||
<StackLayout> | ||
<ScrollView x:DataType="local:PageViewModel22714"> | ||
<StackLayout BindableLayout.ItemsSource="{Binding Items}"> | ||
<BindableLayout.ItemTemplate> | ||
<DataTemplate x:DataType="local:ItemViewModel22714"> | ||
<Label Text="{Binding Title}" /> | ||
</DataTemplate> | ||
</BindableLayout.ItemTemplate> | ||
</StackLayout> | ||
</ScrollView> | ||
|
||
<ScrollView x:DataType="local:PageViewModel22714"> | ||
<StackLayout BindableLayout.ItemsSource="{Binding Items}"> | ||
<BindableLayout.ItemTemplate> | ||
<DataTemplate> | ||
<Label Text="{Binding Title}" /> | ||
</DataTemplate> | ||
</BindableLayout.ItemTemplate> | ||
</StackLayout> | ||
</ScrollView> | ||
</StackLayout> | ||
</ContentPage> |
71 changes: 71 additions & 0 deletions
71
src/Controls/tests/Xaml.UnitTests/Issues/Maui22714.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,71 @@ | ||
using Microsoft.Maui.ApplicationModel; | ||
using Microsoft.Maui.Dispatching; | ||
|
||
using Microsoft.Maui.Controls.Core.UnitTests; | ||
using Microsoft.Maui.UnitTests; | ||
using NUnit.Framework; | ||
|
||
namespace Microsoft.Maui.Controls.Xaml.UnitTests; | ||
|
||
[XamlCompilation(XamlCompilationOptions.Skip)] | ||
public partial class Maui22714 | ||
{ | ||
public Maui22714() | ||
{ | ||
InitializeComponent(); | ||
} | ||
|
||
public Maui22714(bool useCompiledXaml) | ||
{ | ||
//this stub will be replaced at compile time | ||
} | ||
|
||
[TestFixture] | ||
public class Test | ||
{ | ||
[SetUp] | ||
public void Setup() | ||
{ | ||
Application.SetCurrentApplication(new MockApplication()); | ||
DispatcherProvider.SetCurrent(new DispatcherProviderStub()); | ||
} | ||
|
||
[TearDown] public void TearDown() => AppInfo.SetCurrent(null); | ||
|
||
[Test] | ||
public void TestNonCompiledResourceDictionary( | ||
[Values(false, true)] bool useCompiledXaml, | ||
[Values(false, true)] bool treatWarningsAsErrors) | ||
{ | ||
if (useCompiledXaml) | ||
{ | ||
if (treatWarningsAsErrors) | ||
{ | ||
Assert.Throws( | ||
new BuildExceptionConstraint(22, 32, static msg => msg.Contains(" XC0024 ", System.StringComparison.Ordinal)), | ||
() => MockCompiler.Compile(typeof(Maui22714), treatWarningsAsErrors: true)); | ||
} | ||
else | ||
{ | ||
MockCompiler.Compile(typeof(Maui22714), treatWarningsAsErrors: false); | ||
} | ||
} | ||
else | ||
{ | ||
// This will never affect non-compiled builds | ||
_ = new Maui22714(useCompiledXaml); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public class PageViewModel22714 | ||
{ | ||
public string Title { get; set; } | ||
public ItemViewModel22714[] Items { get; set; } | ||
} | ||
|
||
public class ItemViewModel22714 | ||
{ | ||
public string Title { get; set; } | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are there any other types of scopes we would need future changes for?
Wondering if any of these have an issue:
ResourceDictionary
ControlTemplate
In theory, you could have a
{Binding}
inside aStyle
in a{ResourceDictionary}
and the type could be different based on the XAML using theStyle
? Maybe the existing warning already works for this case.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this applies to ResourceDictionary (BTW we're not compiling these bindings at the moment: #22023), but it might apply to ControlTemplate 🤔 In general, this should be whenever there's a different BindingContext in the outer scope and in the inner scope. What do you think, @StephaneDelcroix?