![](/img/trans.png)
[英]Can I inherit from a class if base class doesn't have constructor
[英]MAUI ContentView can't inherit from custom base class
我有一個名為 HomePageOrientationViewLoader 的 ContentView,我想在名為 HomePage 的 ContentPage 中使用它。 如果方向是橫向,HomePageOrientationViewLoader 將加載一個名為 HomePageLandscape 的 ContentView,如果方向是縱向,則加載一個名為 HomePagePortrait 的 ContentView。
我這樣做是為了可以為橫向和縱向加載不同的布局,以便優化我的布局。 這可行,但我想為我的應用程序中的每個頁面做一個自定義 ContentView,我不想每次都重復 HomePageOrientationViewLoader 中的代碼,所以我想創建一個名為 OrientationContentViewLoader 的頁面,它繼承自 ContentView,然后是我的每個頁面ContentView 頁面根據方向處理要加載的 ContentView 將從 OrientationContentViewLoader 繼承。
我有問題,但它不工作。 這是我的代碼:
方向內容視圖加載器:
using System;
using ScoreKeepersBoard.ViewModels;
namespace ScoreKeepersBoard.ContentViews;
public class OrientationContentViewLoader : ContentView
{
public ContentView portraitContentView;
public ContentView landscapeContentView;
public OrientationContentViewLoader()
{
try
{
var viewModel = this.BindingContext;
if(viewModel != null)
{
portraitContentView.BindingContext = viewModel;
landscapeContentView.BindingContext = viewModel;
}
}
catch (Exception e)
{
string message = e.Message;
}
DeviceDisplay.Current.MainDisplayInfoChanged += Current_MainDisplayInfoChanged;
this.Content = DeviceDisplay.Current.MainDisplayInfo.Orientation == DisplayOrientation.Portrait ? portraitContentView : landscapeContentView;
}
private void Current_MainDisplayInfoChanged(object sender, DisplayInfoChangedEventArgs e)
{
var viewModel = this.BindingContext;
landscapeContentView.BindingContext = viewModel;
portraitContentView.BindingContext = viewModel;
if (e.DisplayInfo.Orientation == DisplayOrientation.Landscape)
{
this.Content = landscapeContentView;
}
else if (e.DisplayInfo.Orientation == DisplayOrientation.Portrait)
{
this.Content = portraitContentView;
}
else
{
this.Content = portraitContentView;
}
}
protected override void OnBindingContextChanged()
{
var viewModel = this.BindingContext;
if(viewModel != null)
{
landscapeContentView.BindingContext = viewModel;
portraitContentView.BindingContext = viewModel;
}
}
}
HomePageOrientationViewLoader.xaml.cs:
using System.Reflection;
using ScoreKeepersBoard.ViewModels;
namespace ScoreKeepersBoard.ContentViews;
public partial class HomePageOrientationViewLoader : OrientationContentViewLoader
{
public HomePageOrientationViewLoader()
{
portraitContentView = new HomePagePortrait();
landscapeContentView = new HomePageLandscape();
}
}
這是我的 HomePageOrientationViewLoader.xaml:
<?xml version="1.0" encoding="utf-8" ?>
<local:OrientationContentViewLoader
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ScoreKeepersBoard.ContentViews.HomePageOrientationViewLoader">
<VerticalStackLayout>
<Label
Text="Welcome to .NET MAUI!"
VerticalOptions="Center"
HorizontalOptions="Center" />
</VerticalStackLayout>
</local:OrientationContentViewLoader>
此 XAML 已更改,因為最初對於 HomePageOrientationViewLoader.xaml.cs 我收到錯誤消息:
CS0263: Partial declarations of 'type' must not specify different base classes
我發現這篇 Stackoverflow 帖子說需要更改 XAML 標記: 讓 MAUI 頁面繼承自定義基礎 class
這是 HomePageOrientationViewLoader.xaml 的原始 XAML
<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ScoreKeepersBoard.ContentViews.HomePageOrientationViewLoader">
<VerticalStackLayout>
<Label
Text="Welcome to .NET MAUI!"
VerticalOptions="Center"
HorizontalOptions="Center" />
</VerticalStackLayout>
</ContentView>
這是 HomePage.xaml,它是調用 HomePageOrientationViewLoader 控件的 ContentPage 的 XAML:
<?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:controls="clr-namespace:ScoreKeepersBoard.ContentViews"
x:Class="ScoreKeepersBoard.Views.HomePage"
Title="HomePage">
<VerticalStackLayout>
<controls:HomePageOrientationViewLoader>
</controls:HomePageOrientationViewLoader>
</VerticalStackLayout>
</ContentPage>
我在 HomePageOrientationViewLoader.xaml.cs 上的錯誤似乎已經消失,我現在只是在 HomePageOrientationViewLoader.xaml 上收到錯誤:
在 HomePage.xaml 中出現錯誤:
我以為我是從其他 stackoverflow 帖子復制相同的標記模式,但它似乎沒有用。 我將不勝感激一些指導。
添加xmlns:local="clr-namespace:ScoreKeepersBoard.ContentViews"
到 HomePageOrientationViewLoader.xaml:
<?xml version="1.0" encoding="utf-8" ?>
<local:OrientationContentViewLoader
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:ScoreKeepersBoard.ContentViews"
x:Class="ScoreKeepersBoard.ContentViews.HomePageOrientationViewLoader">
...
</local:OrientationContentViewLoader>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.