标签: winui-3

WinUI 3 调用 ApplicationData.Current() 抛出 System.TypeInitializationException

我正在尝试将 Windows 桌面应用程序从 WinUI 2 转换为 WinUI 3。但是,我遇到了 Windows.Storage.ApplicationData 类的问题。当我调用 ApplicationData.Current(以便我可以访问应用程序本地数据存储中的文件或获取本地设置)时,会引发 System.TypeInitializationException 异常。

下面的截图显示了问题:

系统类型初始化异常

(这是我在 Visual Studio 2022 中使用项目模板“空白应用程序,打包(桌面中的 WinUI 3)”创建的默认应用程序,我所做的唯一更改是添加行以获取 App 类中的当前 ApplicationData 对象OnLaunched() 方法:

var appData = Windows.Storage.ApplicationData.Current;)

知道我在这里做错了什么吗?我认为可能存在信任或能力问题,或者我应该使用其他方法来获取应用程序的本地数据文件夹或 WinUI 3 桌面应用程序的设置。

c# winui-3

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

WinUI 3 UWP - 对话框

我正在 WinUI3 UWP 中为 Windows 11 开发一个新应用程序,我想显示一个对话框来提供安全操作,如 Microsoft Docs 示例中所示:https ://learn.microsoft.com/en-us/windows/apps/设计/控件/对话框和弹出窗口/对话框

private async void DisplayNoWifiDialog()
{
    ContentDialog noWifiDialog = new ContentDialog
    {
        Title = "No wifi connection",
        Content = "Check your connection and try again.",
        CloseButtonText = "Ok"
    };

    ContentDialogResult result = await noWifiDialog.ShowAsync();
}
Run Code Online (Sandbox Code Playgroud)

当用户单击 MainPage.xaml 上的按钮时显示该对话框,当用户单击“确定”时返回到 MainPage.xaml,但是当我运行程序时出现此错误:“必须为无父级弹出窗口显式设置 XamlRoot”

我该如何解决这个问题?

谢谢你!

uwp winui-3 visual-studio-2022 windows-11

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

无法加载清单设计器,PhonePublisherId 丢失

与 Microsoft Store 关联后,当我打开清单时,它无法打开。并显示此错误。这是什么意思?如何纠正这个问题?我在打开清单时从未遇到过 UWP 应用程序的此类错误。有点奇怪。我使用的是 Visual Studio 2022 版本 17.0.5。

在此输入图像描述

winui-3

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

获取应用版本信息时出现奇怪的信息

我有一个 winui 3 应用程序,我使用此代码来获取应用程序版本信息:

assembly = Assembly.GetEntryAssembly();
assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion
Run Code Online (Sandbox Code Playgroud)

之前,我有 Windows 11 - 22H2 和 Visual Studio 2022 v17.7 + net7.0 并且上述代码的输出如下:

4.0.0
Run Code Online (Sandbox Code Playgroud)

今天我将 Windows(全新安装)更改为 23H2 和 Visual Studio 2022 v17.8 /Visual Studio 2022 v18.0-Preview1 + net8.0 但是当我运行我的应用程序(来自 Visual Studio)时,上述代码的输出几乎是如下:

4.0.0 + ea32asd354ad5a4d5a4sda5a54da54da4das54da4d
Run Code Online (Sandbox Code Playgroud)

我将 .net 版本更改为 net7.0 或者移至以前的版本(来自 git)并且输出很奇怪。但是当我从 MS 商店运行我的应用程序时,输出是正确的,或者当我创建新项目时,输出是正确的,但我不知道为什么我的项目被选中

.net c# .net-assembly winui-3

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

WinUI XAML:使用另一个项目中的 ResourceDictionary

在我的 WinUI 3 应用程序中,我尝试使用位于另一个项目中的 ResourceDictionary。

假设引用的项目是ResourceTestLib,该库项目有一个文件夹“Styles”,其中有一个文件“_Thickness.xaml”。

app.xaml主应用程序的文件中,我在“此处的其他合并字典”注释下方尝试了这两种方法,但它们似乎都不起作用,即该应用程序在启动时崩溃,并显示消息“无法找到资源......”消息。

这是我的 app.xaml:

<Application.Resources>
    <ResourceDictionary>

        <ResourceDictionary.MergedDictionaries>
            <XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />

            <!-- Other merged dictionaries here -->
            <ResourceDictionary Source="/ResourceTestLib;component/Styles/_Thickness.xaml"/>
            <ResourceDictionary Source="pack://application:,,,/ResourceTestLib;component/Styles/_Thickness.xaml"></ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>

    </ResourceDictionary>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)

当我将鼠标悬停在相应的行上时,Visual Studio 会显示一个工具提示,其中第一个引用的行显示“路径 X 未找到”,第二个引用的行显示ResourceDictionary“路径中的字符无效”(可能是因为“应用程序:,,,”)消息。

我认为 WinUI XAML 在这方面可能与 WPF XAML 不同?或者甚至还不支持这一点?

c# xaml uwp-xaml winui-3 windows-app-sdk

3
推荐指数
1
解决办法
2113
查看次数

绑定不更新 WinUI 3

我将 WinUI 与 microsoft MVVM 工具包结合使用。但是,我在绑定方面遇到了一些问题,并且无法找出问题所在。

ViewModel 和 ViewModel 中使用的模型属于 observableObject 类型。命令被触发,数据被获取。但是,除非我更改 xaml 并热重新加载更改,否则绑定不会在 UI 中显示结果。

我的页面:

<Page
x:Class="ThrustmasterGuide.Pages.WheelBasePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ThrustmasterGuide.Pages"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:model="using:ThrustmasterGuide.DataAccess.Context.Model"
xmlns:wheelbase="using:ThrustmasterGuide.ViewModel.Wheelbase"
xmlns:xaml="using:ABI.Microsoft.UI.Xaml"
xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:converters="using:ThrustmasterGuide.Converters"
xmlns:wheelBase="using:ThrustmasterGuide.Model.WheelBase"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance wheelbase:WheelBaseViewModel, IsDesignTimeCreatable=True}">
<Page.Resources>
    <converters:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" />
    <converters:InvertBoolToVisibilityConverter x:Key="InvertBoolToVisibilityConverter" />
</Page.Resources>
<interactivity:Interaction.Behaviors>
    <core:EventTriggerBehavior EventName="Loaded">
        <core:EventTriggerBehavior.Actions>
            <core:InvokeCommandAction Command="{x:Bind ViewModel.LoadWheelBaseCommand}" />
        </core:EventTriggerBehavior.Actions>
    </core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>

<StackPanel Padding="16 16 16 16" Orientation="Vertical">
    <StackPanel>
        <TextBlock FontSize="18" Text="{x:Bind ViewModel.WheelBase.Name}" />
        <TextBlock FontSize="18" Text="Symptomen:" />
        <TextBlock Text="Kies hieronder een symptoom uit om te starten." …
Run Code Online (Sandbox Code Playgroud)

data-binding xaml mvvm uwp winui-3

3
推荐指数
2
解决办法
3163
查看次数

WinUI3 和拖放

我正在尝试使用 WinUI3 进行拖放操作。在 WPF 中,我曾经将AllowDropDrop添加到 UI 元素中,并且能够接收已删除文件的文件名。然而,在 WinUI3 中,Drop 方法并未被执行。我究竟做错了什么?

XAML

<Page
    x:Class="..."
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="..."
    xmlns:helper="..."
    xmlns:prop="..."
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
  <Page.Resources>
    <DataTemplate x:Key="Image"  x:DataType="helper:ProductImage">
      <Border BorderBrush="{x:Bind Path=Color, Mode=OneWay}" BorderThickness="1" Background="Transparent"
              AllowDrop="True" Drop="Image_Drop" >
        <Grid Width="{Binding ElementName=PIImageSize, Path=Value}" 
              Height="{Binding ElementName=PIImageSize, Path=Value}" >
          <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
          </Grid.RowDefinitions>
          <TextBlock Grid.Row="0" Margin="2" HorizontalAlignment="Center"
                     Text="{x:Bind Path=Type, Mode=OneWay}" 
                     Foreground="{x:Bind Path=Color, Mode=OneWay}" />
          <Image Grid.Row="1" Stretch="Uniform" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Source="{x:Bind Path=BitmapImage, Mode=OneWay}"/>
          <TextBox Grid.Row="2" …
Run Code Online (Sandbox Code Playgroud)

drag-and-drop winui-3

3
推荐指数
1
解决办法
1292
查看次数

如何在.Net MAUI 中为 ContentPage 标题添加样式?

我正在使用 .NET MAUI 创建桌面应用程序。

在这里,我有一个ContentPage标题为My title 的标题。

<ContentPage
    x:Class="MAUI.Desktop.Pages.PlanningPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    Title="My title">
....
Run Code Online (Sandbox Code Playgroud)

它的标题默认左对齐,你知道如何将其对齐到页面中心吗?如果可以的话,我也想给它添加更多的风格。 在此输入图像描述

编辑:建议的解决方案

  • 我忘记提及我的页面已被考虑NavigationPage。所以,这是我的解决方案:
<ContentPage
    x:Class="MAUI.Desktop.Pages.PlanningPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
>
 <NavigationPage.TitleView>
        <Label
            x:Name="pageTitleView"
            HorizontalTextAlignment="Center"
            Text="My title"/>
</NavigationPage.TitleView>
......
Run Code Online (Sandbox Code Playgroud)

在代码隐藏xaml.cs文件中:

 protected override void OnSizeAllocated(double width, double height)
 {
     base.OnSizeAllocated(width, height);
     pageTitleView.WidthRequest = width;
 }
Run Code Online (Sandbox Code Playgroud)

.net winui-3 maui maui-windows

3
推荐指数
1
解决办法
4914
查看次数

generic.xaml 文件位于哪里?

如果要使用默认样式、颜色等...,则需要从WindowsAppSDK NuGet 包附带的generic.xaml文件中获取它们。

因此问题是, generic.xaml文件位于何处?

xaml generic.xaml winui-3 windows-app-sdk

3
推荐指数
1
解决办法
917
查看次数

更新到.net8后,在xaml编辑器中引用System.Windows.Controls

.NET RID从.NET8更改而来,因此我修改了csproj文件中的PropertyGroup,如下所示。

<PropertyGroup>
  <OutputType>WinExe</OutputType>
  <TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>
  <TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
  <RootNamespace>Test</RootNamespace>
  <ApplicationManifest>app.manifest</ApplicationManifest>
  <Platforms>x64</Platforms>
  <RuntimeIdentifiers>win-x64</RuntimeIdentifiers>
  <PublishProfile>win-$(Platform).pubxml</PublishProfile>
  <UseWinUI>true</UseWinUI>
  <EnableMsixTooling>true</EnableMsixTooling>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

我还修改了名称并删除了 pubxml 文件。

之后,在xaml编辑器中,最初正常引用了Microsoft.UI.Xaml.Controls命名空间,但在某个时候,不知道为什么,引用System.Windows.Controls命名空间出现了错误,并且自动补全不起作用。

在此输入图像描述

在此输入图像描述

错误没有描述,但是通过鼠标向上查看Grid的类型,您可以看到它是“System.Windows.Controls.Grid”。

当我编译它时,它工作正常,但由于编辑器中的错误,很难编码。

有谁知道为什么会发生这个错误?

.net c# xaml winui-3

3
推荐指数
1
解决办法
1868
查看次数