【问题标题】:Get clicked button on lostfocus event在 lostfocus 事件上获得点击按钮
【发布时间】:2016-09-13 08:34:53
【问题描述】:

我有一个 UWP 应用程序,它有一系列带有点击事件的按钮。我还有一个文本框,我需要将注意力集中在应用程序上的几乎每一次点击/触摸上。所以我设置了一个这样的resetFocus函数:

public void resetfocus2(object sender, RoutedEventArgs e)
{
    Username.Focus(FocusState.Programmatic);
}

在此文本框外的每次点击时触发。

这是 XAML 中的按钮

<ListView>
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel Margin="4" Orientation="Vertical">
                <StackPanel Orientation="Horizontal">
                    <Button Tag="{Binding id}" Grid.Column="0" Padding="0" BorderThickness="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="100" Background="White" Click="Meal1_Click">
                        <Image Width="100" Height="100" Source="{Binding idImg}" RelativePanel.AlignLeftWithPanel="True" />
                    </Button>
                </StackPanel>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

我的问题是:

如何调用通常通过单击此函数内部的按钮调用的函数?

因为 lostfocus 事件发生在点击之前,所以点击功能永远不会被触发。

更新: 这是从按钮创建对话框的代码:

public async void Meal1_Click(object sender, RoutedEventArgs e)
    {
        ServiziSoapClient service = new ServiziSoapClient();
        var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
        var mealresponse = await service.SERVICE("PASSWORD", (sender as Button).Tag.ToString(), (string)localSettings.Values["stabilimento"]);

        var response = JsonValue.Parse(mealresponse.Body.SERVICE).GetObject();

        var notnull = 0;
        try
        {
            var temp = response.GetNamedArray("OPTION");
        }
        catch (Exception exc)
        {
            Debug.WriteLine("Exception " + exc.Message);
            notnull = 1;
        }

        JsonArray allergeni = null;
        var piatto = response.GetNamedObject("OPTION2");
        if (notnull == 0)
            allergeni = response.GetNamedArray("OPTION");

        var ingredienti = response.GetNamedString("OPTION3");

        var btn = sender as Button;
        var dialog = new ContentDialog()
        {
            Title = piatto.GetNamedString("descr"),
            //RequestedTheme = ElementTheme.Dark,
            //FullSizeDesired = true,
            MaxWidth = this.ActualWidth // Required for Mobile!
        };

        // Setup Content
        var panel = new StackPanel();

        var defaultImageUri = new Uri("ms-appx:///Images/ic_placeholder_primi.png");
        var bitmap = new BitmapImage();
        bitmap.ImageFailed += (s, ex) => bitmap.UriSource = defaultImageUri;
        bitmap.UriSource = new Uri("http://test-wsmensa.sinergest.it//WS/getImage.ashx?GETIN=B&&@pp2015Gi@BoS&ID_IMMAGINE=" + piatto.GetNamedNumber("idImg") + "&HEIGHT=100", UriKind.Absolute);

        panel.Children.Add(new Image
        {
            Height = 400,
            Width = 400,
            Source = bitmap
        });


        panel.Children.Add(new TextBlock
        {
            TextWrapping = TextWrapping.Wrap,
            FontSize = 20,
            Text = "Ingredienti: " + ingredienti
        });
        dialog.Content = panel;

        dialog.PrimaryButtonText = "Chiudi";
        // Show Dialog
        var result = await dialog.ShowAsync();
        UserName.Focus(FocusState.Programmatic);
    }

【问题讨论】:

  • 不确定 UWP 是否也有此功能,但在常规 WPF 中,我只需在按钮上设置 Focusable = "False"。这样你的文本框就不会失去焦点。

标签: c# xaml uwp uwp-xaml


【解决方案1】:

您似乎希望在单击或点击按钮时按钮不会获​​得焦点。如果是这样,您可以将IsTabStop property 设置为false,使按钮无法接收输入焦点。

如果 IsTabStopfalse,则该控件将从选项卡导航中排除。另外,如果IsTabStopfalse,则控件无法接收输入焦点。 (如果您尝试以编程方式设置焦点,通过调用 Focus 方法,Focus 会返回 false

因此您可以更改您的 XAML 代码,例如:

<Button IsTabStop="False" Tag="{Binding id}" Grid.Column="0" Padding="0" BorderThickness="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="100" Background="White" Click="Meal1_Click">

在此之后,按钮将不会获得焦点,如果您的文本框获得焦点,则当用户单击或点击按钮时它将保持焦点。

【讨论】:

  • 完美。正是我想要的!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-13
  • 1970-01-01
  • 2013-01-03
  • 1970-01-01
相关资源
最近更新 更多