小编Jul*_*ian的帖子

以编程方式添加控件,UI 未更新

我只是在玩.Net MAUI。我想从 Rest 服务检索信息,并根据结果以编程方式将按钮添加到 VerticalStackLayout。当我调试解决方案时,按钮会添加到 VerticalStackLayout 但 UI 不会更新。

这是代码片段

var  btn  = new Button();
btn.Text = "Button " + count + " added";
btn.Clicked += OnCounterClicked;
btn.HorizontalOptions = LayoutOptions.Center;
VLayout.Add(btn);      
Run Code Online (Sandbox Code Playgroud)

这里是 XAML

<ScrollView>
    <VerticalStackLayout x:Name="VLayout"
        Spacing="25" 
        Padding="30,0" 
        VerticalOptions="Center">
        <Image
            Source="dotnet_bot.png"
            SemanticProperties.Description="Cute dot net bot waving hi to you!"
            HeightRequest="200"
            HorizontalOptions="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" />
        <Entry …
Run Code Online (Sandbox Code Playgroud)

maui

12
推荐指数
1
解决办法
5756
查看次数

.NET MAUI:如何确保 Android 平台特定代码仅在支持的 Android 版本上执行?

我有各种实现来partial class DeviceServices为 Android、iOS 等提供某些设备或操作系统特定功能的平台特定实现。我的应用程序的目标 API 级别为 33.0,最低版本为 API 级别 21.0。

某些 API 特定于某些 Android 版本及更高版本,因此我想确保仅在受支持的版本上调用它们。但是,我总是收到以下警告(以及类似的警告,具体取决于所使用的 API):

警告 CA1416:可在“Android”21.0 及更高版本上访问此调用站点。“WindowInsets.Type.SystemBars()”仅支持:“android”30.0 及更高版本。

以下隐藏和显示系统栏的代码适用于我迄今为止尝试过的所有设备和模拟器,但我担心早期的 Android 版本。尽管检查了正确的目标 API 版本,我仍然收到上面的警告:

static partial class DeviceServices
{
    private static Activity _activity;

    public static void SetActivity(Activity activity)
    {
        _activity = activity;
    }

    public static partial void HideSystemControls()
    {
#if ANDROID30_0_OR_GREATER
       if (Build.VERSION.SdkInt >= BuildVersionCodes.R) //R == API level 30.0
       {
            _activity?.Window?.InsetsController?.Hide(WindowInsets.Type.SystemBars());
       }
#endif
    }

    public static partial void ShowSystemControls()
    {
#if ANDROID30_0_OR_GREATER
       if (Build.VERSION.SdkInt >= …
Run Code Online (Sandbox Code Playgroud)

c# android conditional-compilation android-api-levels maui

7
推荐指数
2
解决办法
6728
查看次数

标签控件的 MaxLine 和 LineBreakMode 属性无法正常工作

为了限制标签的最大行数并指示文本截断,我们使用MaxLines和控件LineBreakMode的属性Label

这在 Xamarin.Forms 中工作正常,但在 .NET MAUI 中不起作用。

主页.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"
             x:Class="Maui_POC.MainPage">

    <ScrollView>
        <StackLayout
            Spacing="25"
            Padding="30,0"
            VerticalOptions="Center">

            <Image
                Source="dotnet_bot.png"
                SemanticProperties.Description="Cute dot net bot waving hi to you!"
                HeightRequest="200"
                HorizontalOptions="Center" />

            <Label
                Text="Hello, World!"
                SemanticProperties.HeadingLevel="Level1"
                FontSize="32"
                HorizontalOptions="Center" />

            <Label
                Text="Welcome to .NET Multi-platform App UI, Cute dot net bot waving hi to you!"
                SemanticProperties.HeadingLevel="Level2"
                FontSize="18"
                MaxLines="2"
                LineBreakMode="TailTruncation"
                HorizontalOptions="Center" />

            <Button
                x:Name="CounterBtn"
                Text="Click me"
                SemanticProperties.Hint="Counts the number of times you click"
                Clicked="OnCounterClicked" …
Run Code Online (Sandbox Code Playgroud)

maui

6
推荐指数
1
解决办法
3736
查看次数

MAUI:如何在 SingleProject 中将部分类与 net7.0 作为 TargetFramework 一起用于特定于平台的实现?

我正在使用部分类在 .NET MAUI 应用程序中实现特定于平台的行为:

干:

public partial class MyServices
{
    public partial void DoSomething();
}
Run Code Online (Sandbox Code Playgroud)

Android/iOS/MacCatalyst/Windows/Tizen 特定实现都与此类似:

public partial class MyServices
{
    public partial void DoSomething()
    {
        // Android/iOS/MacCatalyst/Windows/Tizen specific implementation
    }
}
Run Code Online (Sandbox Code Playgroud)

到目前为止,对于 MAUI 来说很正常(尽管特定于平台的实现可以以不同的方式完成,但是部分类方法对于 MAUI 来说很常见并且看起来很方便)。

现在,为了能够执行单元测试 (xUnit),需要将目标添加net7.0SingleProject.csproj<TargetFrameworks>文件中,如下所示:

<PropertyGroup>
    <TargetFrameworks>net7.0;net7.0-android;net7.0-ios;net7.0-maccatalyst</TargetFrameworks>
    <TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net7.0-windows10.0.19041.0</TargetFrameworks>

    <!-- skipping irrelevant stuff here... -->

    <OutputType Condition="'$(TargetFramework)' != 'net7.0'">Exe</OutputType>
    
    <!-- skipping irrelevant stuff here... -->
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

这正如 Gerald Versluis 在他的YouTube 视频中所描述的那样。相关代码示例可以在这里找到:https://github.com/jfversluis/MauixUnitTestSample/blob/main/MauixUnitTestSample/MauixUnitTestSample.csproj#L5

这就是我的问题开始的地方:

由于net7.0目标和类的缺失实现 …

.net c# maui .net-maui

6
推荐指数
1
解决办法
2095
查看次数

.NET MAUI 应用程序中的渐变页面背景

如何在 .NET MAUI 应用程序中将页面设置为具有线性背景?

我尝试定义LinearGradientBrushin Colors.xaml,然后将其分配为StaticResource- 见下文 - 但这似乎不起作用。

在 中Colors.xaml,我有这个:

<LinearGradientBrush x:Key="PageGradientBackground" EndPoint="1,1">
     <GradientStop
         Color="{StaticResource UIOffWhite}"
         Offset="0.1"/>
     <GradientStop
         Color="{StaticResource UILightGray}"
         Offset="0.6"/>
     <GradientStop
         Color="{StaticResource UIMidGray}"
         Offset="1.0"/>
</LinearGradientBrush>
Run Code Online (Sandbox Code Playgroud)

然后像这样使用它:

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             BackgroundColor="{StaticResource PageGradientBackground}">
    <!-- ... -->
</ContentPage>
Run Code Online (Sandbox Code Playgroud)

我也尝试过内联定义它,但这也不起作用。这实际上是不允许的:

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
    <ContentPage.BackgroundColor>
        <LinearGradientBrush>
        </LinearGradientBrush>
    </ContentPage.BackgroundColor>
    <!-- ... -->
</ContentPage>
Run Code Online (Sandbox Code Playgroud)

知道如何为 a 提供渐变背景ContentPage吗?

xaml maui

6
推荐指数
1
解决办法
3009
查看次数

Firebase 身份验证失败并出现 FirebaseException:JsonResponse 实例化失败

问题

我刚刚为我的一个应用程序创建了更新Xamarin.Forms,现在我遇到了一个问题:将新的.aab上传到 Google Play后,Firebase登录工作流程突然中断。到目前为止一直运行良好。

当我尝试登录时,选择 Google 用户帐户后,设备日志中会显示以下错误消息:

Time    Device Name Type    PID Tag Message
11-18 16:49:57.295  Samsung SM-S901B    Verbose 5646    mono-stdout com.google.firebase.FirebaseException: An internal error has occurred. [ Instantiation of JsonResponse failed! class com.google.android.gms.internal.firebase-auth-api.zzaac ]
    at com.google.android.gms.internal.firebase-auth-api.zzwe.zza(com.google.firebase:firebase-auth@@21.0.8:4)
    at com.google.android.gms.internal.firebase-auth-api.zzxc.zza(com.google.firebase:firebase-auth@@21.0.8:9)
    at com.google.android.gms.internal.firebase-auth-api.zzxd.zzl(com.google.firebase:firebase-auth@@21.0.8:1)
    at com.google.android.gms.internal.firebase-auth-api.zzxa.zzh(com.google.firebase:firebase-auth@@21.0.8:25)
    at com.google.android.gms.internal.firebase-auth-api.zzwc.zzh(com.google.firebase:firebase-auth@@21.0.8:1)
    at com.google.android.gms.internal.firebase-auth-api.zzua.zza(com.google.firebase:firebase-auth@@21.0.8:2)
    at com.google.android.gms.internal.firebase-auth-api.zzxl.zzb(com.google.firebase:firebase-auth@@21.0.8:13)
    at com.google.android.gms.internal.firebase-auth-api.zzxl.zza(com.google.firebase:firebase-auth@@21.0.8:14)
    at com.google.android.gms.internal.firebase-auth-api.zzwr.zzq(com.google.firebase:firebase-auth@@21.0.8:4)
    at com.google.android.gms.internal.firebase-auth-api.zzuh.zzA(com.google.firebase:firebase-auth@@21.0.8:4)
    at com.google.android.gms.internal.firebase-auth-api.zzwd.zzu(com.google.firebase:firebase-auth@@21.0.8:5)
    at com.google.android.gms.internal.firebase-auth-api.zzvj.zzc(com.google.firebase:firebase-auth@@21.0.8:1)
    at com.google.android.gms.internal.firebase-auth-api.zzxe.run(com.google.firebase:firebase-auth@@21.0.8:1)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637)
    at java.lang.Thread.run(Thread.java:1012)
Run Code Online (Sandbox Code Playgroud)

这仅影响由 Google Play 签名的发布版本,因此,我无法在调试器中复制它。运行调试版本时,一切都按预期工作。

附加信息

Android 版本:13.0 …

android xamarin.forms firebase-authentication google-signin

6
推荐指数
3
解决办法
1452
查看次数

.NET MAUI 导航动画

如果我想在 MAUI 中为从一个页面到另一页面的过渡设置动画,我需要使用true值激活它:

await Shell.Current.GoToAsync($"//{nameof(DashboardPage)}", true);
Run Code Online (Sandbox Code Playgroud)

这会动画化页面从右到左的过渡。有没有办法反转转换 => 从左到右?有什么建议么?我在 MAUI 文档中没有看到这个选项。有什么窍门吗?

.net navigation animation maui

6
推荐指数
1
解决办法
4872
查看次数

如何解析在 ViewModel 内的 builder.Services 中注册的服务?

摘要:我希望使用 MAUIbuilder.Services来解析 ViewModel 中的服务,但我不明白该怎么做。

我可以创建自己的IServiceProvider,但我希望避免所需的样板代码,因此我寻求“标准 MAUI”解决方案。

我添加了以下行MauiProgram.CreateMauiApp()

builder.Services.AddSingleton<IAlertService, AlertService>();
Run Code Online (Sandbox Code Playgroud)

以及相应的声明(在其他文件中):

public interface IAlertService
{
    // ----- async calls (use with "await") -----
    Task ShowAlertAsync(string title, string message, string cancel = "OK");
    Task<bool> ShowConfirmationAsync(string title, string message, string accept = "Yes", string cancel = "No");
}

internal class AlertService : IAlertService
{
    // ----- async calls (use with "await") -----

    public Task ShowAlertAsync(string title, string message, string cancel = "OK")
    {
        return Application.Current.MainPage.DisplayAlert(title, message, cancel); …
Run Code Online (Sandbox Code Playgroud)

maui

5
推荐指数
3
解决办法
8673
查看次数

MAUI:在页面之间导航的 GoToAsync() 和 PushAsync() 之间有什么区别?

我正在尝试了解如何在 MAUI 应用程序和 MVVM 的页面之间导航的最佳方式。

我在 Microsoft 的示例中看到,默认的操作方式是使用PushAsync(),但后来我发现可以使用 进行导航Shell.Current.GoToAsync()

两者有什么区别?或者说它们是互补的?

c# mvvm maui

5
推荐指数
1
解决办法
1992
查看次数

MAUI 调整活动规模

我正在尝试使用 MAUI 作为 Windows 应用程序,该应用程序需要在从宽屏幕到平板电脑的多种屏幕尺寸上工作,并允许调整窗口大小。

我需要能够检测窗口调整大小事件,并根据窗口的大小有条件地显示输出。例如,在宽屏幕上显示完整网格,但在较小屏幕上显示卡片。

SizeChanged我已经实现了MAUI 应用程序(https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/app-lifecycle )的一个事件,可以在应用程序级别记录更改。

using Microsoft.Maui.LifecycleEvents;
    
public static MauiApp CreateMauiApp()
{
    var builder = MauiApp.CreateBuilder();
    builder
          .UseMauiApp<App>()
          .ConfigureLifecycleEvents(events =>
          {
#if WINDOWS
              events.AddWindows(windows => windows
                     .OnWindowCreated(window =>
                     {
                            window.SizeChanged += OnSizeChanged;
                     }));
#endif
          });
    
    return builder.Build();
}  

#if WINDOWS
static void OnSizeChanged(object sender, Microsoft.UI.Xaml.WindowSizeChangedEventArgs args)
{
    ILifecycleEventService service = MauiWinUIApplication.Current.Services.GetRequiredService<ILifecycleEventService>();
    service.InvokeEvents(nameof(Microsoft.UI.Xaml.Window.SizeChanged));
}
#endif
Run Code Online (Sandbox Code Playgroud)

但是我如何将其链接到单独的 MAUI 页面,以便我可以适当地检测新的窗口大小和布局?

任何建议或更好的解决方案将不胜感激

window-resize maui

4
推荐指数
1
解决办法
3980
查看次数