Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Maui Navigation RoutedViewHost (#3574)
- Loading branch information
1 parent
36c40d7
commit 5b186ce
Showing
18 changed files
with
899 additions
and
171 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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright (c) 2023 .NET Foundation and Contributors. All rights reserved. | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for full license information. | ||
|
||
#if HAS_WINUI | ||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq; | ||
using Microsoft.UI.Xaml; | ||
using Microsoft.UI.Xaml.Controls; | ||
using Microsoft.UI.Xaml.Markup; | ||
|
||
namespace ReactiveUI | ||
{ | ||
/// <summary> | ||
/// AutoDataTemplateBindingHook is a binding hook that checks ItemsControls | ||
/// that don't have DataTemplates, and assigns a default DataTemplate that | ||
/// loads the View associated with each ViewModel. | ||
/// </summary> | ||
public class AutoDataTemplateBindingHook : IPropertyBindingHook | ||
{ | ||
/// <summary> | ||
/// Gets the default item template. | ||
/// </summary> | ||
[SuppressMessage("Design", "CA1307: Use the currency locale settings", Justification = "Not available on all platforms.")] | ||
public static Lazy<DataTemplate> DefaultItemTemplate { get; } = new(() => | ||
{ | ||
const string template = "<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' " + | ||
"xmlns:xaml='clr-namespace:ReactiveUI;assembly=__ASSEMBLYNAME__'> " + | ||
"<xaml:ViewModelViewHost ViewModel=\"{Binding Mode=OneWay}\" VerticalContentAlignment=\"Stretch\" HorizontalContentAlignment=\"Stretch\" IsTabStop=\"False\" />" + | ||
"</DataTemplate>"; | ||
var assemblyName = typeof(AutoDataTemplateBindingHook).Assembly.FullName; | ||
assemblyName = assemblyName?.Substring(0, assemblyName.IndexOf(',')); | ||
return (DataTemplate)XamlReader.Load(template.Replace("__ASSEMBLYNAME__", assemblyName)); | ||
}); | ||
|
||
/// <inheritdoc/> | ||
public bool ExecuteHook(object? source, object target, Func<IObservedChange<object, object>[]> getCurrentViewModelProperties, Func<IObservedChange<object, object>[]> getCurrentViewProperties, BindingDirection direction) | ||
{ | ||
if (getCurrentViewProperties is null) | ||
{ | ||
throw new ArgumentNullException(nameof(getCurrentViewProperties)); | ||
} | ||
|
||
var viewProperties = getCurrentViewProperties(); | ||
var lastViewProperty = viewProperties.LastOrDefault(); | ||
|
||
if (lastViewProperty?.Sender is not ItemsControl itemsControl) | ||
{ | ||
return true; | ||
} | ||
|
||
if (!string.IsNullOrEmpty(itemsControl.DisplayMemberPath)) | ||
{ | ||
return true; | ||
} | ||
|
||
if (viewProperties.Last().GetPropertyName() != "ItemsSource") | ||
{ | ||
return true; | ||
} | ||
|
||
if (itemsControl.ItemTemplate is not null) | ||
{ | ||
return true; | ||
} | ||
|
||
if (itemsControl.ItemTemplateSelector is not null) | ||
{ | ||
return true; | ||
} | ||
|
||
itemsControl.ItemTemplate = DefaultItemTemplate.Value; | ||
return true; | ||
} | ||
} | ||
} | ||
#endif |
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,31 @@ | ||
// Copyright (c) 2023 .NET Foundation and Contributors. All rights reserved. | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
|
||
namespace ReactiveUI | ||
{ | ||
/// <summary> | ||
/// Enum that hints at the visibility of a ui element. | ||
/// </summary> | ||
[Flags] | ||
public enum BooleanToVisibilityHint | ||
{ | ||
/// <summary> | ||
/// Do not modify the boolean type conversion from it's default action of using the Visibility.Collapsed. | ||
/// </summary> | ||
None = 0, | ||
|
||
/// <summary> | ||
/// Inverse the action of the boolean type conversion, when it's true collapse the visibility. | ||
/// </summary> | ||
Inverse = 1 << 1, | ||
|
||
/// <summary> | ||
/// Use the hidden version rather than the Collapsed. | ||
/// </summary> | ||
UseHidden = 1 << 2, | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
src/ReactiveUI.Maui/Common/BooleanToVisibilityTypeConverter.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,70 @@ | ||
// Copyright (c) 2023 .NET Foundation and Contributors. All rights reserved. | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
#if HAS_MAUI | ||
using Microsoft.Maui; | ||
#endif | ||
#if HAS_WINUI | ||
using Microsoft.UI.Xaml; | ||
#endif | ||
|
||
namespace ReactiveUI | ||
{ | ||
/// <summary> | ||
/// This type convert converts between Boolean and XAML Visibility - the | ||
/// conversionHint is a BooleanToVisibilityHint. | ||
/// </summary> | ||
public class BooleanToVisibilityTypeConverter : IBindingTypeConverter | ||
{ | ||
/// <inheritdoc/> | ||
public int GetAffinityForObjects(Type fromType, Type toType) | ||
{ | ||
if (fromType == typeof(bool) && toType == typeof(Visibility)) | ||
{ | ||
return 10; | ||
} | ||
|
||
if (fromType == typeof(Visibility) && toType == typeof(bool)) | ||
{ | ||
return 10; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public bool TryConvert(object? from, Type toType, object? conversionHint, out object result) | ||
{ | ||
var hint = conversionHint is BooleanToVisibilityHint visibilityHint ? | ||
visibilityHint : | ||
BooleanToVisibilityHint.None; | ||
|
||
if (toType == typeof(Visibility) && from is bool fromBool) | ||
{ | ||
var fromAsBool = (hint & BooleanToVisibilityHint.Inverse) != 0 ? !fromBool : fromBool; | ||
|
||
#if !NETFX_CORE && !HAS_UNO && !HAS_WINUI | ||
var notVisible = (hint & BooleanToVisibilityHint.UseHidden) != 0 ? Visibility.Hidden : Visibility.Collapsed; | ||
#else | ||
var notVisible = Visibility.Collapsed; | ||
#endif | ||
result = fromAsBool ? Visibility.Visible : notVisible; | ||
return true; | ||
} | ||
|
||
if (from is Visibility fromAsVis) | ||
{ | ||
result = fromAsVis == Visibility.Visible ^ (hint & BooleanToVisibilityHint.Inverse) == 0; | ||
} | ||
else | ||
{ | ||
result = Visibility.Visible; | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
} |
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,16 @@ | ||
// Copyright (c) 2023 .NET Foundation and Contributors. All rights reserved. | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for full license information. | ||
|
||
namespace ReactiveUI | ||
{ | ||
/// <summary> | ||
/// Returns the current orientation of the device on Windows. | ||
/// </summary> | ||
public class PlatformOperations : IPlatformOperations | ||
{ | ||
/// <inheritdoc/> | ||
public string? GetOrientation() => null; | ||
} | ||
} |
Oops, something went wrong.