从WPF窗口导航到Razor页面(Blazor Hybrid)

uqdfh47h  于 2023-05-19  发布在  其他
关注(0)|答案(1)|浏览(190)

我有一个使用Wpf + Blazor的dotnet-7 windows应用程序。
我的问题是我无法从window导航到razor page
我的MainWindow.xaml

<Button Content="Open" Click="OpenOnClick" />

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        var serviceCollection = new ServiceCollection();
        serviceCollection.AddWpfBlazorWebView();
#if DEBUG
        serviceCollection.AddBlazorWebViewDeveloperTools();
#endif
        Resources.Add("services", serviceCollection.BuildServiceProvider());

        InitializeComponent();
    }
    private void OpenOnClick(object sender, RoutedEventArgs e)
    {
        new EditingWindow().Show();
    }
}

EditingWindow.xaml

<Window x:Class="CodeEditor.EditingWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:blazor="clr-namespace:Microsoft.AspNetCore.Components.WebView.Wpf;assembly=Microsoft.AspNetCore.Components.WebView.Wpf"
        xmlns:local="clr-namespace:CodeEditor"
        mc:Ignorable="d"
        Title="EditingPage" Height="450" Width="800">
    <Grid>
        <blazor:BlazorWebView HostPage="wwwroot/index.html" Services="{DynamicResource services}">
            <blazor:BlazorWebView.RootComponents>
                <blazor:RootComponent Selector="#app" ComponentType="{x:Type local:IndexEditor}" />
            </blazor:BlazorWebView.RootComponents>
        </blazor:BlazorWebView>
    </Grid>
</Window>

而razor页面本身只是一个<h1>标签和后面的空代码:
IndexEditor.razorIndexEditor.razor.cs
index.html是常规的blazor样板:

...head

<body>

<div id="app">Loading...</div>

<div id="blazor-error-ui">
    An unhandled error has occurred.
    <a href="" class="reload">Reload</a>
    <a class="dismiss">🗙</a>
</div>

<script src="_framework/blazor.webview.js" autostart="false"></script>

</body>

当我点击按钮,窗口显示出来,但它是空的,它有我在EditingWindow.Xaml中设置的标题,这是EditingPage,但它是空的,我认为问题是它不能服务于剃刀页面,但IDK为什么,我也尝试了不同的导航方式,但没有一个有任何区别。

zhte4eai

zhte4eai1#

您需要将“services”ServiceProvider资源添加到托管BlazorWebViewEditingWindow

public partial class EditingWindow : Window
{
    public EditingWindow()
    {
        InitializeComponent();

        var serviceCollection = new ServiceCollection();
        serviceCollection.AddWpfBlazorWebView();
        Resources.Add("services", serviceCollection.BuildServiceProvider());
    }
}

相关问题