现在,MAUI 为我们提供了所有 Essentials API 的接口,我正尝试在我的代码中使用它们,以使事情更具可测试性。
在这段视频中,詹姆斯说我们应该将它们注册为:
builder.Services.AddSingleton<IGeolocation, GeolocationImplementation>();
Run Code Online (Sandbox Code Playgroud)
但现在实现类都是内部的,因此不能以这种方式注册。
现在在类上使用 Current 或 Default 属性的正确方法如下所示?
builder.Services.AddSingleton<IGeolocation>(ctx => Geolocation.Default);
builder.Services.AddSingleton<IConnectivity>(ctx => Connectivity.Current);
Run Code Online (Sandbox Code Playgroud)
谢谢
在 .NET MAUI 中运行 Xunit 项目时出现“程序不包含适合入口点的静态‘主’方法”错误
public class UnitTest1
{
[Fact]
public void Test1()
{
}
}
Run Code Online (Sandbox Code Playgroud)
重现:
使用 Visual Studio 17.3 预览版 2.0
我有一个带有页面 A 和页面 B 的 .Net MAUI 应用程序。
我想从页面 A >>> 页面 B 移至页面。
当我使用以下代码移动时,我有一个后退按钮(这是我的情况下所需的行为):
await Navigation.PushAsync(new MyPage());
Run Code Online (Sandbox Code Playgroud)
但是,如果我使用以下代码移动到其他页面,则没有后退按钮:
await Shell.Current.GoToAsync("//MyPage");
Run Code Online (Sandbox Code Playgroud)
由于其他一些原因,我需要通过 Shell 使用导航。
为什么使用 Shell 导航时没有后退按钮?
我正在使用 dot NET MAUI Blazor 来创建我的新应用程序。我正在努力让图像发挥作用。我在谷歌上找不到任何东西。我不确定我是否应该使用<img ImageSource="">或<img src="">什么都不起作用?我是否需要添加任何类型的权限,或者我应该使用什么来显示图像,我尝试过:
ImageSource="Images/snack.png"
ImageSource="snack.png"
src="images/snack.png"
src="snack.png"
Run Code Online (Sandbox Code Playgroud)
我确实已包含在我的 .csprog 文件中并且也尝试过但没有任何效果
我有一个 .NET MAUI 应用程序,其中一个页面在 MauiProgram.cs 中注册为瞬态。我希望能够使用页面本身上的按钮重置页面,但不知道如何操作。请有人帮助我。
我已经尝试过使用这段代码,但它没有做任何事情:
[RelayCommand]
public async Task ReloadPageAsync()
{
await Shell.Current.GoToAsync("../"+nameof(MyPage), false);
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试在 .NET MAUI 中构建自定义控件,该控件应为其父级提供 ICommand 的 BindableProperty。这是我试图实现的目标的一个基本示例。
主页视图 (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"
xmlns:views="clr-namespace:SampleApp.Views"
x:Class="SampleApp.MainPage">
<views:MyCustomControl DoSomething="{Binding DoSomethingCommand}"></views:MyCustomControl>
</ContentPage>
Run Code Online (Sandbox Code Playgroud)
MainPage 视图类 (MainPage.xaml.cs)
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
BindingContext = new MainPageViewModel();
}
}
Run Code Online (Sandbox Code Playgroud)
MainPage 视图模型 (MainPageViewModel.cs)
public class MainPageViewModel : ObservableObject
{
public ICommand ProcessNewScoreCommand { get; }
public MainPageViewModel()
{
ProcessNewScoreCommand = new Command(DoSomething);
}
private void DoSomething()
{
// Do something
}
}
Run Code Online (Sandbox Code Playgroud)
MyCustomControl 视图类 (MyCustomControl.xaml.cs)
public partial class …Run Code Online (Sandbox Code Playgroud) 这听起来像是一个新手或愚蠢的问题(很可能是)。
但我创建了一个非常简单的 .NET MAUI 应用程序,几乎与默认模板相同,略有修改。它在编辑器中运行良好(默认调试,任何 cpu 配置),但构建后不会运行 .exe。
构建是正确的并且生成了 .EXE,但是当尝试运行它时(无论管理员权限如何)它不会打开任何窗口。检查了任务管理器,它打开了一个名为 exe 的任务,该任务立即终止。
关于为什么会发生这种情况有什么想法吗?
提前致谢。
我刚刚开始尝试在 Visual Studio 中使用 MAUI 来开发多平台应用程序。我已经成功地使用了 shell 导航,并且使用弹出按钮效果很好。
我想做的是点击一个按钮,导航到另一个页面并传递一个变量。
什么起作用(有点)是这样的:
await Navigation.PushModalAsync(new ResultListView(ordnum));
Run Code Online (Sandbox Code Playgroud)
这确实导航到页面并根据需要传递 var ordnum,但是它脱离了 shell 导航(我松开了弹出窗口等)。
我不知道如何在 Shell 中执行此操作
await Shell.Current.GoToAsync
Run Code Online (Sandbox Code Playgroud)
似乎是我想要的,但是当我使用路由时,我无法以相同的方式指定变量?
这似乎是一个基本的问题,我确信以前曾被问过,但老实说我找不到它。我花了整个早上寻找答案!我发现的有关通过 XAML 传递变量的答案非常令人困惑。
谢谢
安德鲁
我正在尝试通过两个按钮在毛伊岛创建自定义控件。
每当开发人员想要在页面中插入控件时,就会显示两个按钮,他可以添加一些特定的操作作为处理程序。
public partial class BottomButtons : Grid
{
public BottomButtons()
{
InitializeComponent();
}
public BindableProperty LeftButtonTextProperty = BindableProperty.Create(propertyName: nameof(LeftButtonText),
returnType: typeof(String),
defaultValue: "Cancel",
defaultBindingMode: BindingMode.OneWay,
declaringType: typeof(BottomButtons)
);
public String LeftButtonText
{
get => (String)GetValue(LeftButtonTextProperty);
set { SetValue(LeftButtonTextProperty, value); }
}
public BindableProperty RightButtonTextProperty = BindableProperty.Create(propertyName: nameof(RightButtonText),
returnType: typeof(String),
defaultValue: "Save",
defaultBindingMode: BindingMode.OneWay,
declaringType: typeof(BottomButtons)
);
public String RightButtonText
{
get => (String)GetValue(RightButtonTextProperty);
set { SetValue(RightButtonTextProperty, value); }
}
public BindableProperty ClickActionProperty = BindableProperty.Create(propertyName: nameof(ClickAction),
returnType: typeof(EventHandler),
defaultValue: null, …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 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 页面,以便我可以适当地检测新的窗口大小和布局?
任何建议或更好的解决方案将不胜感激