标签: .net-maui

.NET MAUI 中的 Shell 导航到带有底部选项卡的页面

在登录流程中,登录页面通常没有构成应用程序主要流程的底部选项卡。

AppShell.xaml

 <TabBar>
        <ShellContent Title="Home"
             Icon="home.png" 
                      ContentTemplate="{DataTemplate local:HomePage}"/>
        <ShellContent Title="Articles"
                          Icon="articles.png"
                          ContentTemplate="{DataTemplate local:ArticlesPage}" />
    </TabBar>
Run Code Online (Sandbox Code Playgroud)

因此,如果登录成功,我尝试从登录页面导航到作为 Shell 中 TabBar 的一部分的主页。问题是 Shell 然后导航到主页,就好像它是一个独立的页面,没有 TabBar。我假设答案在于导航到 TabBar 部分本身,我不知道。

maui .net-maui

7
推荐指数
1
解决办法
7927
查看次数

调整大小程序 检测到一个或多个无效文件名

我无法消除 Visual Studio for Mac 2022 v17 中 .net maui 应用程序中的此生成错误?构建输出显示:

/usr/local/share/dotnet/packs/Microsoft.Maui.Resizetizer.Sdk/6.0.300-rc.3.5667/targets/Microsoft.Maui.Resizetizer.targets(511,9): error :             One or more invalid file names were detected.  File names must be lowercase, start and end with a letter character, and contain only alphanumeric characters or underscores: 
Run Code Online (Sandbox Code Playgroud)

当我将图像文件复制到资源/图像文件夹中时,它们确实包含无效字符,但我将文件重命名为仅包含有效的小写字母数字字母,但 Visual Studio 仍然损坏。我已经清理了项目,手动删除了 bin 和 obj 文件夹,重新启动了解决方案和 Visual Studio,甚至启动了整个机器,但没有成功。我什至尝试删除所有图像,但没有成功。

必须有一些其他缓存仍然保存着错误消息的无效引用,该错误消息根本就是错误的,并且由于其他一些相关原因而被抛出。任何帮助发现这个问题的帮助将不胜感激。

编辑 我现在创建了一个新项目(MyMauiSolution),并一一复制旧解决方案中的文件夹,以查看它会在哪里损坏。将所有文件复制到新项目后,它仍然有效。所以现在我有 2 个解决方案,它们或多或少具有完全相同的文件/图像,其中一个可以工作,另一个则不能?!

我什至尝试过比较文件夹结构,但没有区别?!

myiMac:Projects user_x$ diff -rq BlueWhaleMaui MyMauiSolution
Files BlueWhaleMaui/.DS_Store and MyMauiSolution/.DS_Store differ
Only in BlueWhaleMaui: .git
Only in BlueWhaleMaui: .gitignore
Only in BlueWhaleMaui: BlueWhaleMaui …
Run Code Online (Sandbox Code Playgroud)

visual-studio-mac .net-maui

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

在 .NET MAUI 应用程序中创建和使用 HttpClient

我们是否在 .NET MAUI 应用程序中创建/使用 an HttpClient,就像在 Web 或 API 应用程序中一样?具体来说,我将我的HttpClient' 添加到我的应用程序中Program.cs使用IHttpFactory-- 见下文:

builder.Services.AddHttpClient("NamedClient1", config =>
{
    var url = currentState == "production"
        ? "https://my-production-url"
        : "https://sandbox-url";
    config.BaseAddress = new Uri(url);
});
Run Code Online (Sandbox Code Playgroud)

我理解 .NET MAUI 的想法是标准化我们处理事物的方式。当我尝试在 .NET MAUI 应用程序中添加该行时builder.Services,IntelliSense 并不建议AddHttpClient

xamarin xamarin.forms maui .net-maui

7
推荐指数
1
解决办法
1万
查看次数

.Net MAUI 如何在标签中包含链接

使用 Visual Studio 社区版 2022。

.Net MAUI 新手,我正在尝试遵循https://learn.microsoft.com/en-us/dotnet/maui/user-interface/controls/label上的文档中提供的示例

下面的 XAML 代码(来自文档)用于使用点击识别器创建“链接”:

     <Label>
        <Label.FormattedText>
           <FormattedString>
              <Span 
                 Text="Link: " 
              />
              <Span 
                 Text="click here to open the docs"
                 TextColor="Blue"
                 TextDecorations="Underline">
                 <Span.GestureRecognizers>
                    <TapGestureRecognizer
                       Command="{Binding OpenUrlCommand}"
                       CommandParameter="https://learn.microsoft.com/dotnet/maui/" 
                    />
                 </Span.GestureRecognizers>
              </Span>
           </FormattedString>
        </Label.FormattedText>
     </Label>
Run Code Online (Sandbox Code Playgroud)

但是,该命令 (OpenUrlCommand) 从未被调用 - 使用 Windows 和 Andriod 模拟器进行测试。

在我的 ViewModel 中,我定义 OpenUrlCommand 如下:

public ICommand OpenUrlCommand => new Command<string>( async ( url ) => await Launcher.OpenAsync( url ) );
Run Code Online (Sandbox Code Playgroud)

...但是没有任何效果,我也尝试了以下方法 - 不行...

public ICommand OpenUrlCommand => new Command<string>( async ( …
Run Code Online (Sandbox Code Playgroud)

c# .net-maui maui-community-toolkit

7
推荐指数
1
解决办法
1万
查看次数

为什么我无法在 Visual Studio 2022 上编译 .NET MAUI 应用程序?

我想开始使用 .NET MAUI 进行编码,因此我将 VS2022 更新为“Version 17.1.0 Preview 6.0”
然后我按照https://learn.microsoft.com/en-us/dotnet/maui/get-started/installation中的建议使用“Visual Studio Installer”安装了这些功能

在此输入图像描述

不幸的是,当我创建一个基本的 MAUI 应用程序时,我遇到了一些如下错误:

一个或多个目标框架不存在错误 NU1012 平台版本,即使它们已指定平台:net6.0-android

在开发人员命令行上,我尝试运行dotnet workload list并得到
android
ios
maui-android
maui-maccatalyst

如果我尝试安装其他一些工作负载(例如 maui 或 maui-windows),我会收到一条错误消息

无法下载清单 microsoft.net.workload.emscripten:在 NuGet 源https://api.nuget.org/v3/index.json上未找到 microsoft.net.workload.emscripten.manifest-6.0.200 ,C: \Program Files (x86)\Microsoft SDKs\NuGetPackages"

我也尝试过完全删除并重新安装 VS2022,但没有任何运气。
然后我尝试运行,maui-check但当它尝试下载建议:下载 .NET SDK (6.0.200)时,exe 无效并且任务失败。

最糟糕的部分:我尝试在另一台电脑上安装新的 VS2022 预览版,一切都完美运行...但我需要它在我的开发电脑上运行!

visual-studio-2022 .net-maui

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

dotnet 发布错误

我使用这些资源来学习如何发布 MAUI 应用程序:

\n

https://learn.microsoft.com/en-us/dotnet/maui/ios/deployment/overview

\n

https://github.com/dotnet/maui/issues/4397

\n

https://github.com/dotnet/sdk/issues/21877

\n

基于这些,我尝试了不同的变体来发布,但它们都不起作用:

\n
dotnet publish -f:net6.0-ios -c:Release /p:RuntimeIdentifier=ios-arm64 \n
Run Code Online (Sandbox Code Playgroud)\n
\n

错误:运行时标识符“ios-arm64”无效。

\n
\n
dotnet publish -f:net6.0-ios -c:Release /p:RuntimeIdentifier=ios-arm64 --no-restore\n
Run Code Online (Sandbox Code Playgroud)\n
\n

错误 NETSDK1032:RuntimeIdentifier 平台“ios-arm64”和\nPlatformTarget“x64”必须兼容。

\n
\n
dotnet build -f:net6.0-ios -c:Release /p:RuntimeIdentifier=ios-arm64 /p:BuildIpa=true\n
Run Code Online (Sandbox Code Playgroud)\n
\n

错误:运行时标识符“ios-arm64”无效。

\n
\n
dotnet build -f:net6.0-ios -c:Release /p:RuntimeIdentifier=ios-arm64 /p:BuildIpa=true --no-restore\n
Run Code Online (Sandbox Code Playgroud)\n
\n

错误 NETSDK1032:RuntimeIdentifier 平台“ios-arm64”和\nPlatformTarget“x64”必须兼容。

\n
\n

更新
\n环境:
\nWindows 10 家庭版 - 21H2
\nVS 2022 版本 17.3.0 预览版 1.1

\n

更新2
\n根据答案,我现在有了这个 csproj 文件:

\n …

maui .net-maui

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

如何在.NET MAUI中显示相机预览?

目前,我所做的是访问相机并用它来拍照,但这并不是我想要完成的。

我想在内容页面上激活并显示前置摄像头预览,然后通过按按钮控件拍照。

我四处搜寻,似乎找不到任何解决方案。在Xamarin中,Xamarin Community Toolkit中有CameraView可用于相机流,但从外观上看,CameraView尚未在.NET MAUI Community Toolkit中实现。还有其他方法可以做到这一点吗?谢谢。

maui .net-maui

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

为什么 .NET-Maui 全局样式不起作用?

我已在 .Net Maui 中声明了全局样式并尝试从其中一个页面访问它,但它抛出异常

Microsoft.Maui.Controls.Xaml.XamlParseException:位置 10:37。类型转换器失败:调用目标已引发异常。Microsoft.Maui.Controls.Xaml.XamlParseException:位置 8:34。未找到主键的 StaticResource。

应用程序.xaml代码

<?xml version = "1.0" encoding = "UTF-8" ?>
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:MyApp"
         x:Class="MyApp.App">
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Resources/Styles/Colors.xaml" />
            <ResourceDictionary Source="Resources/Styles/Styles.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
    <Style x:Key="redLabelStyle"
           TargetType="Label">
        <Setter Property="TextColor"
                    Value="Red"/>
        <Setter Property="FontSize"
                    Value="Small"/>
        <Setter Property="FontAttributes"
                    Value="Bold"/>
    </Style>

    <Style TargetType="Label">
        <Setter Property="TextColor"
                    Value="Green"/>
        <Setter Property="FontSize"
                    Value="Small"/>
        <Setter Property="FontAttributes"
                    Value="Bold"/>
    </Style>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)

MainPage.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"
         NavigationPage.HasNavigationBar="False"
         x:Class="MyApp.MainPage">

<VerticalStackLayout HorizontalOptions="CenterAndExpand"
                     VerticalOptions="CenterAndExpand">
    <Label Style="{StaticResource redLabelStyle}"
           Text="Global Style Red Label"/>
    <Label …
Run Code Online (Sandbox Code Playgroud)

xamarin xamarin.forms maui .net-maui

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

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
查看次数

无法将图像添加到 Mac 上的新 .NET MAUI 项目中,除非获得:名称“资源”已保留,无法使用

我刚刚在 Mac 上启动了一个全新的模板 .NET MAUI 项目,并且能够毫无问题地构建和运行启动项目。当我将任何图像添加到“Resources/Images”文件夹,然后尝试构建项目时,我收到错误:

错误描述: 名称“资源”已被保留,无法使用。

错误路径: Resources/Images/icon_notes.png

规格 Visual Studio for Mac 17.4 预览版(17.4 内部版本 2326)

我尝试过清理和重建项目,但这没有帮助。

重现步骤:

  1. 安装 Visual Studio for Mac 17.4 预览版
  2. 从 IDE 提供的启动模板创建新的 .NET MAUI 项目
  3. 构建并运行以确保它正常运行(它会)。
  4. 将任何图像添加到“Resources/Images”文件夹中,然后尝试重建。

请帮助我了解这里出了什么问题以及如何解决它。

这是我的 csproj 文件

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFrameworks>net6.0-android;net6.0-ios;net6.0-maccatalyst</TargetFrameworks>
        <TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net6.0-windows10.0.19041.0</TargetFrameworks>
        <!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET -->
        <!-- <TargetFrameworks>$(TargetFrameworks);net6.0-tizen</TargetFrameworks> -->
        <OutputType>Exe</OutputType>
        <RootNamespace>Notes</RootNamespace>
        <UseMaui>true</UseMaui>
        <SingleProject>true</SingleProject>
        <ImplicitUsings>enable</ImplicitUsings>

        <!-- Display name -->
        <ApplicationTitle>Notes</ApplicationTitle>

        <!-- App …
Run Code Online (Sandbox Code Playgroud)

visual-studio .net-maui

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