This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
An alternate xaml resource file provider can request the XamlLoader to _not_ ignore normally ignored properties in prebuilt XF design xmlns, as in the following snippet: ```xaml <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"" xmlns:d="http://xamarin.com/schemas/2014/forms/design"" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"" mc:Ignorable="d""> <Label d:Text="Bar" Text="Foo" x:Name="label" /> </ContentPage> ``` The `d:` should be the prefix used by default for this, but any other prefix will do too. The `d:Text` property maps to the exact same property as `Text`, as the XmllnsDefinitionAttributes are identical (that's convenient from a Intelisense point of view), but, when (and only when) instructed by a provided Xaml resource loader, the `d:Text` will override the `Text` property. This works with virtually all properties defined on built-in Xamarin.Forms controls, but it doesn't mean it's a sane idea to try to assign design value to all existing properties. The APi ofr setting the ResourceLoader had to change, and instead of taking pre-defined arguments, it accepts and returns query and response types. This is slightly less convenient to invoke through reflection, but way more easy to extend in the future.
- Loading branch information
1 parent
7199cd0
commit a206fa0
Showing
10 changed files
with
230 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,56 @@ | ||
using System; | ||
using System.ComponentModel; | ||
using System.Reflection; | ||
|
||
namespace Xamarin.Forms.Internals | ||
{ | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
public static class ResourceLoader | ||
{ | ||
static Func<AssemblyName, string, string> resourceProvider; | ||
|
||
[Obsolete("You shouldn't have used this one to begin with, don't use the other one either")] | ||
//takes a resource path, returns string content | ||
public static Func<AssemblyName, string, string> ResourceProvider { | ||
get => resourceProvider; | ||
internal set { | ||
DesignMode.IsDesignModeEnabled = true; | ||
resourceProvider = value; | ||
if (value != null) | ||
ResourceProvider2 = rlq => new ResourceLoadingResponse { ResourceContent = value(rlq.AssemblyName, rlq.ResourcePath) }; | ||
else | ||
ResourceProvider2 = null; | ||
} | ||
} | ||
|
||
static Func<ResourceLoadingQuery, ResourceLoadingResponse> _resourceProvider2; | ||
public static Func<ResourceLoadingQuery, ResourceLoadingResponse> ResourceProvider2 { | ||
get => _resourceProvider2; | ||
internal set { | ||
DesignMode.IsDesignModeEnabled = value != null; | ||
_resourceProvider2 = value; | ||
} | ||
} | ||
|
||
[Obsolete("Can't touch this")] | ||
public static bool CanProvideContentFor(ResourceLoadingQuery rlq) | ||
{ | ||
if (_resourceProvider2 == null) | ||
return false; | ||
return _resourceProvider2(rlq) != null; | ||
} | ||
|
||
public class ResourceLoadingQuery | ||
{ | ||
public AssemblyName AssemblyName { get; set; } | ||
public string ResourcePath { get; set; } | ||
} | ||
|
||
public class ResourceLoadingResponse | ||
{ | ||
public string ResourceContent { get; set; } | ||
public bool UseDesignProperties { get; set; } | ||
} | ||
|
||
internal static Action<Exception> ExceptionHandler { 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
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,34 @@ | ||
using System; | ||
using NUnit.Framework; | ||
using Xamarin.Forms.Core.UnitTests; | ||
|
||
namespace Xamarin.Forms.Xaml.UnitTests | ||
{ | ||
[TestFixture] | ||
public class DesignPropertiesTests | ||
{ | ||
[SetUp] public void Setup() => Device.PlatformServices = new MockPlatformServices(); | ||
[TearDown] public void TearDown() => Device.PlatformServices = null; | ||
|
||
[Test] | ||
public void DesignProperties() | ||
{ | ||
var xaml = @" | ||
<ContentPage | ||
xmlns=""http://xamarin.com/schemas/2014/forms"" | ||
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" | ||
xmlns:d=""http://xamarin.com/schemas/2014/forms/design"" | ||
xmlns:mc=""http://schemas.openxmlformats.org/markup-compatibility/2006"" | ||
mc:Ignorable=""d""> | ||
<Label d:Text=""Bar"" Text=""Foo"" x:Name=""label"" /> | ||
</ContentPage>"; | ||
|
||
var view = new ContentPage(); | ||
XamlLoader.Load(view, xaml, useDesignProperties: true); //this is equiv as LoadFromXaml, but with the bool set | ||
|
||
var label = ((Forms.Internals.INameScope)view).FindByName("label") as Label; | ||
|
||
Assert.That(label.Text, Is.EqualTo("Bar")); | ||
} | ||
} | ||
} |
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,48 @@ | ||
using System.Linq; | ||
|
||
namespace Xamarin.Forms.Xaml | ||
{ | ||
class RemoveDuplicateDesignNodes : IXamlNodeVisitor | ||
{ | ||
public TreeVisitingMode VisitingMode => TreeVisitingMode.TopDown; | ||
public bool StopOnDataTemplate => false; | ||
public bool StopOnResourceDictionary => false; | ||
public bool VisitNodeOnDataTemplate => true; | ||
public bool SkipChildren(INode node, INode parentNode) => false; | ||
public bool IsResourceDictionary(ElementNode node) => false; | ||
|
||
public void Visit(ValueNode node, INode parentNode) | ||
{ | ||
} | ||
|
||
public void Visit(MarkupNode node, INode parentNode) | ||
{ | ||
} | ||
|
||
public void Visit(ElementNode node, INode parentNode) | ||
{ | ||
if (node.Properties == null || node.Properties.Count == 0) | ||
return; | ||
var props = node.Properties.ToList(); | ||
for (var i = 0; i < props.Count; i++) { | ||
var key = props[i].Key; | ||
if (key.NamespaceURI != XamlParser.XFDesignUri) | ||
continue; | ||
var k = new XmlName(XamlParser.XFUri, key.LocalName); | ||
if (node.Properties.Remove(k)) | ||
continue; | ||
if (node.NamespaceResolver.LookupPrefix(XamlParser.XFUri) == "") | ||
node.Properties.Remove(new XmlName("", k.LocalName)); | ||
} | ||
} | ||
|
||
public void Visit(RootNode node, INode parentNode) | ||
{ | ||
Visit((ElementNode)node, parentNode); | ||
} | ||
|
||
public void Visit(ListNode node, INode parentNode) | ||
{ | ||
} | ||
} | ||
} |
Oops, something went wrong.