【问题标题】:UWP Invoce ContentDialog as Service using Dependency InjectionUWP 调用 ContentDialog 作为使用依赖注入的服务
【发布时间】:2018-08-23 21:46:42
【问题描述】:

我正在尝试改编 UWP ContentDialog Invocation 帖子中的示例。 我使用相同的代码,但是当我按下按钮时,只出现一个空框:

另外我的 MainPage.xaml.cs:

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        SpeechDialogService dialog = new SpeechDialogService();
        AbcViewModel = new AbcViewModel(dialog);
    }
    public AbcViewModel AbcViewModel { get; set; }
}

从 MainPage.xaml.cs 中截取一段:

<Page
...
    <Grid>
        <Button Content="Translate" Click="{x:Bind AbcViewModel.Dictate}" />
    </Grid>
</Page>

我做错了什么?感谢您的帮助。

编辑:

我正在使用此服务,它在设计器中看起来不错:

Speech.xaml

<ContentDialog
x:Class="DI_sample.Views.Speech"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:DI_sample.Views"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="Dictate"
PrimaryButtonText="Accept"
SecondaryButtonText="Cancel"
>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="auto"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="150" />
        <ColumnDefinition Width="150" />
    </Grid.ColumnDefinitions>
        <Button Margin="15" Content="Dictate" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Stretch"/>
    <Button  Margin="15" Content="Clear Text" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Stretch"/>
    <TextBlock Grid.Row="1" Grid.ColumnSpan="2" Text="Tap 'Dictate', and speak" FontSize="12" />
        <TextBlock Margin="0 10 0 0" Grid.Row="2" Grid.ColumnSpan="2" Text="Message Dication" HorizontalAlignment="Center" FontSize="24"  />
    <ScrollViewer Grid.Row="3" Grid.ColumnSpan="2" Height="300">
        <TextBox Margin="5 5 5 10"  AcceptsReturn="True"  />
    </ScrollViewer>
</Grid>

我也使用answer to the question I've linked above中的代码:

public interface ISpeechDialogService
{
    Task ShowAsync();
}

public class SpeechDialogService : ISpeechDialogService
{   
    public async Task ShowAsync()
    {
        var contentDialog = new Speech();
        await contentDialog.ShowAsync();
    }
}


public class AbcViewModel
{
    readonly ISpeechDialogService _dialog;

    public AbcViewModel(ISpeechDialogService dialog)
    {
        _dialog = dialog;
    }

    public async void Dictate(object obj)
    {
        await _dialog.ShowAsync();
    }
}

【问题讨论】:

  • 您单独的 ContentDialog XAML 文件在哪里?请在此处发布 xaml 代码。我会帮你检查一下。
  • @XavierXie-MSFT 感谢您的回复,我已添加代码。

标签: c# xaml mvvm dependency-injection uwp


【解决方案1】:

实际上,我在构建您的代码示例时遇到了错误。

XamlCompiler 错误 WMC1121:绑定分配无效:事件“单击”的签名无效。事件只能绑定到与事件签名匹配或无参数的方法

但是,我相信这个问题与您的“空 ContentDialog”问题无关。我刚刚更改了Dictate 方法,如下所示:

public async void Dictate(object sender, RoutedEventArgs e)
{
    await _dialog.ShowAsync();
}

然后,一切正常。你可以看到截图:

您的问题应该在其他地方,但我不确定。请告诉我您项目的目标版本和最低版本,操作系统构建版本。还请提供一个简单的可重现代码示例。您可以上传它并在此处发布链接。我会尽力帮助您诊断此问题。

【讨论】:

  • 我搞砸了我的项目。所以我用相同的设置和相同的代码创建了一个新的(没有 "object sender, RoutedEventArgs e" ),它现在可以工作了。感谢您的帮助。
【解决方案2】:

我只创建了一个简单的 XAML 视图 (Speech.xaml)。但是,需要代码隐藏,因此需要 ContentPage / ContentView (+Speech.xaml.cs)。所以必须用 ContentView 替换 XAML 视图才能解决问题。

添加 Speech.xaml.cs:

    public sealed partial class Speech : ContentDialog
{
    public Speech()
    {
        this.InitializeComponent();
    }
}

【讨论】:

    猜你喜欢
    • 2017-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-12
    • 2020-09-09
    • 1970-01-01
    相关资源
    最近更新 更多