在我的 .NET MAUI MauiProgram.cs 中,我注册了一个 ResourceManager 以及名为 MyContentView 的 ContentView 的代码隐藏(指的是 MyContentView.xaml.cs):
来自 MauiProgram.cs 的部分
...
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.UseMauiCommunityToolkit()
.UseLocalizationResourceManager(settings =>
{
settings.RestoreLatestCulture(true);
settings.AddResource(AppResources.ResourceManager);
})
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});
builder.Services.AddTransient<MyItemInputControlView>();
builder.Services.AddTransient<MyContentPage>();
builder.Services.AddTransient<MyContentPageViewModel>();
Run Code Online (Sandbox Code Playgroud)
MyItemInputControlView.xaml:
<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
xmlns:views="clr-namespace:MyApp.Pages.Views"
xmlns:loc="clr-namespace:LocalizationResourceManager.Maui;assembly=LocalizationResourceManager.Maui"
xmlns:viewModels="clr-namespace:MyApp.ViewModels"
x:Class="MyApp.Pages.Views.MyItemInputControlView"
x:Name="this">
<StackLayout BindingContext="{x:Reference this}">
<Grid Margin="20, 0, 20, 0">
<Grid.Resources>
<!--<Style TargetType="Entry">
<Setter Property="Padding" Value="2 1" />
<Setter Property="BorderBrush" Value="LightGray" />
</Style>-->
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" …Run Code Online (Sandbox Code Playgroud) 我想知道是否可以将 SwiftUI 视图加载到 NSView 中。我希望将 Windows 内容视图设置为 SwiftUI,但
window?.contentView = view视图为 SwiftUI 时似乎不可能
简而言之,我有一个内容视图,例如;
<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="myapp.customstacklayout">
<StackLayout>
<StackLayout>
<StackLayout x:Name="header">
<!-- some title things here, such as a header label etc-->
</StackLayout>
<StackLayout x:Name="content">
<!--Contents should be added here when reused-->
</StackLayout>
<StackLayout x:Name="footer">
<!-- some footer things here, such as a summary label etc-->
</StackLayout>
</StackLayout>
<!--Not here-->
</StackLayout>
</ContentView>
Run Code Online (Sandbox Code Playgroud)
我想在 ContentPage 中重用它,例如;
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:mycontrols="clr-namespace:myapp"
x:Class="myapp.mainpage">
<StackLayout>
<mycontrols:customstacklayout>
<Button Text="TestButton"/>
<Entry Text="TestEntry"/>
<Label Text="TestLabel"/>
.... and etc..
</mycontrols:customstacklayout>
</StackLayout>
</ContentPage>
Run Code Online (Sandbox Code Playgroud)
要创建这样一个可重用的项目,我认为,在 xaml 中,必须有一些内容让 contentview 指出应该将子项添加到哪个 IView 项目中
有什么想法或一段代码吗? …
这是我的内容视图的代码。正如您所看到的,我尝试了使用 HStack 来包含 TextField,以及仅包含 TextField 本身。角半径对灰色搜索矩形没有任何影响 - 边缘仍然是完美的矩形。
@State var searchText = ""
var body: some View {
ZStack {
//function that's the content argument to ZStack
Color((.systemGreen))
VStack {
Text("App")
.font(.largeTitle)
.foregroundColor(Color.white)
//HStack {
TextField("Searchstring", text: $searchText)
.padding()
.background(Color(.systemGray6))
.padding()
.cornerRadius(12)
//}
// .padding()
// .background(Color(.systemGray6))
// .padding(.horizontal)
// .cornerRadius(12)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
在所有情况下,预览都是这样的: 角半径无法在预览中显示