UWP C#视窗小键盘和;文本块绑定

UWP C#视窗小键盘和;文本块绑定,c#,xaml,binding,windows-iot-core-10,C#,Xaml,Binding,Windows Iot Core 10,我正在测试基于UWP示例的键盘和文本块绑定 但是,当我在键盘上按时运行代码时,按下的键不会在文本块上打印 我对示例代码做了一些更改。我已经从示例中添加了ViewModels和Helper。 你能告诉我哪里做错了吗? 谢谢 我的XAML代码如下 <TextBlock x:Name="KeypadDisplay" FontSize="50" TextAlignment="Right" Text="{Binding DialerPhoneNumber.Num

我正在测试基于UWP示例的键盘和文本块绑定

但是,当我在键盘上按时运行代码时,按下的键不会在
文本块上打印

我对示例代码做了一些更改。我已经从示例中添加了ViewModels和Helper。 你能告诉我哪里做错了吗? 谢谢

我的XAML代码如下

<TextBlock x:Name="KeypadDisplay" FontSize="50" TextAlignment="Right"
                   Text="{Binding DialerPhoneNumber.NumberToDial, Mode=OneWay}" 
                   VerticalAlignment="Top" HorizontalAlignment="Left" Height="80" Width="300" Margin="70,20,0,0">
</TextBlock>

<Button Grid.Column="1" Grid.Row="1"
                    Command="{Binding ProcessDialPad}" Style="{StaticResource TextBlockButtonStyle}"
                    CommandParameter="1" Tag="1" HorizontalAlignment="Center" Height="30" Width="100">
                <StackPanel Orientation="Vertical" HorizontalAlignment="Center">
                    <TextBlock Text="1" FontSize="18" FontWeight="Bold" Style="{StaticResource BodyTextBlockStyle}"/>
                </StackPanel>
</Button>
<Button Grid.Column="2" Grid.Row="1"
                    Command="{Binding ProcessDialPad}" Style="{StaticResource TextBlockButtonStyle}"
                    CommandParameter="2" Tag="2" HorizontalAlignment="Center" Height="30" Width="100">
                <StackPanel Orientation="Vertical" HorizontalAlignment="Center">
                    <TextBlock Text="2" FontSize="18" FontWeight="Bold" Style="{StaticResource BodyTextBlockStyle}"/>
                </StackPanel>
</Button>
<Button Grid.Column="3" Grid.Row="1"
                    Command="{Binding ProcessDialPad}" Style="{StaticResource TextBlockButtonStyle}"
                    CommandParameter="3" Tag="3" HorizontalAlignment="Center" Height="30" Width="100">
                <StackPanel Orientation="Vertical" HorizontalAlignment="Center">
                    <TextBlock Text="3" FontSize="18" FontWeight="Bold" Style="{StaticResource BodyTextBlockStyle}"/>
                </StackPanel>
</Button>

我猜您没有为绑定模型的页面设置
DataContext
。请检查是否在页面中添加以下代码

DataContext = ViewModelDispatcher.DialerViewModel;
下面的代码对我来说很好。在这里,我在示例PhoneCall的ViewModels和Helpers中使用了相同的代码

MainPage.xaml

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="100" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <StackPanel Grid.Row="0" HorizontalAlignment="Stretch">
        <Border Background="BlueViolet">
            <TextBlock x:Name="KeypadDisplay" FontSize="50" TextAlignment="Right"  Foreground="White"
               Text="{Binding DialerPhoneNumber.NumberToDial, Mode=OneWay}" 
               VerticalAlignment="Top" HorizontalAlignment="Stretch" Height="100">

            </TextBlock>
        </Border>

    </StackPanel>

    <StackPanel Grid.Row="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
        <Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Height="600">
            <Grid.RowDefinitions>
                <RowDefinition Height="12" />
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
                <RowDefinition Height="12" />
            </Grid.RowDefinitions>

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="12" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="12" />
            </Grid.ColumnDefinitions>

            <Button Grid.Column="1" Grid.Row="1"
            Command="{Binding ProcessDialPad}" Style="{StaticResource DialpadButtonStyle}"
            CommandParameter="1" Tag="1" Holding="OnDialPadHolding">
                <StackPanel Orientation="Vertical">
                    <TextBlock Text="1" Style="{StaticResource DialpadNumberStyle}"/>
                    <FontIcon FontFamily="Segoe MDL2 Assets" 
                      FontWeight="ExtraLight"
                      Glyph="&#xE77C;" 
                      RenderTransformOrigin="0.5,0.5" 
                      Height="14.8">
                        <FontIcon.RenderTransform>
                            <CompositeTransform ScaleX="1" ScaleY="1"/>
                        </FontIcon.RenderTransform>
                    </FontIcon>
                </StackPanel>
            </Button>
            <Button Grid.Column="2" Grid.Row="1" 
            Command="{Binding ProcessDialPad}" Style="{StaticResource DialpadButtonStyle}"
            CommandParameter="2">
                <StackPanel Orientation="Vertical">
                    <TextBlock Text="2" Style="{StaticResource DialpadNumberStyle}"/>
                    <TextBlock Text="ABC" Style="{StaticResource DialpadLetterStyle}"/>
                </StackPanel>
            </Button>
            <Button Grid.Column="3" Grid.Row="1" 
            Command="{Binding ProcessDialPad}" Style="{StaticResource DialpadButtonStyle}"
            CommandParameter="3">
                <StackPanel Orientation="Vertical">
                    <TextBlock Text="3" Style="{StaticResource DialpadNumberStyle}"/>
                    <TextBlock Text="DEF" Style="{StaticResource DialpadLetterStyle}"/>
                </StackPanel>
            </Button>
            <Button Grid.Column="1" Grid.Row="2" 
            Command="{Binding ProcessDialPad}" Style="{StaticResource DialpadButtonStyle}"
            CommandParameter="4">
                <StackPanel Orientation="Vertical">
                    <TextBlock Text="4" Style="{StaticResource DialpadNumberStyle}"/>
                    <TextBlock Text="GHI" Style="{StaticResource DialpadLetterStyle}"/>
                </StackPanel>
            </Button>
            <Button Grid.Column="2" Grid.Row="2" 
            Command="{Binding ProcessDialPad}" Style="{StaticResource DialpadButtonStyle}"
            CommandParameter="5">
                <StackPanel Orientation="Vertical">
                    <TextBlock Text="5" Style="{StaticResource DialpadNumberStyle}"/>
                    <TextBlock Text="JKL" Style="{StaticResource DialpadLetterStyle}"/>
                </StackPanel>
            </Button>
            <Button Grid.Column="3" Grid.Row="2" 
            Command="{Binding ProcessDialPad}" Style="{StaticResource DialpadButtonStyle}"
            CommandParameter="6">
                <StackPanel Orientation="Vertical">
                    <TextBlock Text="6" Style="{StaticResource DialpadNumberStyle}"/>
                    <TextBlock Text="MNO" Style="{StaticResource DialpadLetterStyle}"/>
                </StackPanel>
            </Button>
            <Button Grid.Column="1" Grid.Row="3" 
            Command="{Binding ProcessDialPad}" Style="{StaticResource DialpadButtonStyle}"
            CommandParameter="7">
                <StackPanel Orientation="Vertical">
                    <TextBlock Text="7" Style="{StaticResource DialpadNumberStyle}"/>
                    <TextBlock Text="PQRS" Style="{StaticResource DialpadLetterStyle}"/>
                </StackPanel>
            </Button>
            <Button Grid.Column="2" Grid.Row="3" 
            Command="{Binding ProcessDialPad}" Style="{StaticResource DialpadButtonStyle}"
            CommandParameter="8">
                <StackPanel Orientation="Vertical">
                    <TextBlock Text="8" Style="{StaticResource DialpadNumberStyle}"/>
                    <TextBlock Text="TUV" Style="{StaticResource DialpadLetterStyle}"/>
                </StackPanel>
            </Button>
            <Button Grid.Column="3" Grid.Row="3" 
            Command="{Binding ProcessDialPad}" Style="{StaticResource DialpadButtonStyle}"
            CommandParameter="9">
                <StackPanel Orientation="Vertical">
                    <TextBlock Text="9" Style="{StaticResource DialpadNumberStyle}"/>
                    <TextBlock Text="WXYZ" Style="{StaticResource DialpadLetterStyle}"/>
                </StackPanel>
            </Button>
            <Button Grid.Column="1" Grid.Row="4" 
            Command="{Binding ProcessDialPad}" Style="{StaticResource DialpadButtonStyle}"
            CommandParameter="*" Tag="," Holding="OnDialPadHolding">
                <StackPanel Orientation="Vertical">
                    <TextBlock Text="*" Style="{StaticResource DialpadNumberStyle}"/>
                    <TextBlock Text="," Style="{StaticResource DialpadLetterStyle}"/>
                </StackPanel>
            </Button>
            <Button Grid.Column="2" Grid.Row="4" 
            Command="{Binding ProcessDialPad}" Style="{StaticResource DialpadButtonStyle}"
            CommandParameter="0" Tag="+" Holding="OnDialPadHolding">
                <StackPanel Orientation="Vertical">
                    <TextBlock Text="0" Style="{StaticResource DialpadNumberStyle}"/>
                    <TextBlock Text="+" Style="{StaticResource DialpadLetterStyle}"/>
                </StackPanel>
            </Button>
            <Button Grid.Column="3" Grid.Row="4" 
            Command="{Binding ProcessDialPad}" Style="{StaticResource DialpadButtonStyle}"
            CommandParameter="#" Tag=";" Holding="OnDialPadHolding">
                <StackPanel Orientation="Vertical">
                    <TextBlock Text="#" Style="{StaticResource DialpadNumberStyle}"/>
                    <TextBlock Text=";" Style="{StaticResource DialpadLetterStyle}"/>
                </StackPanel>
            </Button>

        </Grid>
    </StackPanel>
</Grid>

MainPage.cs

    public MainPage()
    {
        this.InitializeComponent();

        DataContext = ViewModelDispatcher.DialerViewModel;
    }

    /// <summary>
    /// Processes press and hold for the buttons that supports press and hold. E.g
    /// 1 -> Voicemail
    /// 0 -> +
    /// * -> , (pause)
    /// # -> ; (wait)
    /// </summary>
    private void OnDialPadHolding(object sender, Windows.UI.Xaml.Input.HoldingRoutedEventArgs e)
    {
        Button button = (Button)sender;
        DialerViewModel vm = (DialerViewModel)DataContext;
        if ((vm != null) && (e.HoldingState == Windows.UI.Input.HoldingState.Started))
        {
            vm.ProcessDialPadHolding.Execute(button.Tag);
        }

    }
public主页()
{
this.InitializeComponent();
DataContext=ViewModelDispatcher.DialServiceWModel;
}
/// 
///处理支持按住的按钮的按住。例如
///1->语音邮件
/// 0 -> +
///*->,(暂停)
/// # -> ; (等等)
/// 
私有void OnDialPadHolding(对象发送器,Windows.UI.Xaml.Input.HoldingRoutedEventArgs e)
{
按钮=(按钮)发送器;
DialServiceWModel vm=(DialServiceWModel)数据上下文;
if((vm!=null)&&(e.HoldingState==Windows.UI.Input.HoldingState.Started))
{
ProcessDialPadHolding.Execute(button.Tag);
}
}

我无法从XAML代码的示例中找到一些不同之处。如果可能的话,你能提供更多关于ProcessDialPad commadn的代码片段吗?@MichaelXu MSFT我在帖子中更新了我的
ProcessDialPad
例程。这是我不明白的部分。一旦我绑定了它,我就不需要调用例程来检查键了,因为它已经绑定了,一旦按下按钮,它就会执行。谢谢。实际上,我省略了
DataContext=ViewModelDispatcher.DialerViewModel
    public MainPage()
    {
        this.InitializeComponent();

        DataContext = ViewModelDispatcher.DialerViewModel;
    }

    /// <summary>
    /// Processes press and hold for the buttons that supports press and hold. E.g
    /// 1 -> Voicemail
    /// 0 -> +
    /// * -> , (pause)
    /// # -> ; (wait)
    /// </summary>
    private void OnDialPadHolding(object sender, Windows.UI.Xaml.Input.HoldingRoutedEventArgs e)
    {
        Button button = (Button)sender;
        DialerViewModel vm = (DialerViewModel)DataContext;
        if ((vm != null) && (e.HoldingState == Windows.UI.Input.HoldingState.Started))
        {
            vm.ProcessDialPadHolding.Execute(button.Tag);
        }

    }