我正在将我的Xamarin表单应用程序转换为.NET MAUI。
我正在尝试将自定义呈现器从Xamarin迁移到MAUI,下面是在.NET MAUI中使用自定义渲染器链接。
这是我的密码:
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.UseMauiCompatibility()
.ConfigureMauiHandlers(handlers =>
{
handlers.AddCompatibilityRenderer(typeof(CustomFrame), typeof(CustomShadowFrameRenderer));
});
return builder.Build();
}CustomFrame:
public class CustomFrame : Frame
{
public CustomFrame()
{
}
}下面是自定义渲染器类
public class CustomShadowFrameRenderer : FrameRenderer
{
public CustomShadowFrameRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Frame> e)
{
base.OnElementChanged(e);
if (e.NewElement != null && e.OldElement == null)
{
e.NewElement.HeightRequest = 1000;
e.NewElement.VerticalOptions = LayoutOptions.FillAndExpand;
}
}<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiApp1.MainPage" BackgroundColor="Red"
xmlns:local="clr-namespace:MauiApp1.Controls">
<local:CustomFrame BackgroundColor="Blue" HeightRequest="1000" Padding="20">
<VerticalStackLayout
Spacing="25"
Padding="30,0"
VerticalOptions="Center">
<Label
Text="Hello, World!"
SemanticProperties.HeadingLevel="Level1"
FontSize="32"
HorizontalOptions="Center" />
<Label
Text="Welcome to .NET Multi-platform App UI"
SemanticProperties.HeadingLevel="Level2"
SemanticProperties.Description="Welcome to dot net Multi platform App U I"
FontSize="18"
HorizontalOptions="Center" />
<Button
x:Name="CounterBtn"
Text="Click me"
SemanticProperties.Hint="Counts the number of times you click"
Clicked="OnCounterClicked"
HorizontalOptions="Center" />
</VerticalStackLayout>
</local:CustomFrame>
</ContentPage>框架不接受毛伊岛的高度要求,但同样的作品在Xamarin形式下也很好。
下面是相同的图片

发布于 2022-11-24 02:23:21
我创建了一个示例来测试您的代码,并遇到了同样的问题。此外,我甚至不能点击CustomFrmae中的按钮。在毛伊河中,FrameRender似乎存在一些兼容性问题。
因此,我尝试将CustomRender更改为CustomHandler。它运行得很好,您只需更改两个位置代码即可。
public class CustomShadowFrameRenderer : Microsoft.Maui.Controls.Handlers.Compatibility.FrameRenderervar builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureMauiHandlers(handlers =>
{
handlers.AddHandler(typeof(CustomFrame), typeof(CustomShadowFrameRenderer));
})https://stackoverflow.com/questions/74549468
复制相似问题