获取lostfocus事件的点击按钮

时间:2016-05-17 09:54:40

标签: c# xaml uwp uwp-xaml

我有一个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);
    }

1 个答案:

答案 0 :(得分:0)

It seems you want the buttons won't get focus when you click or tap the buttons. If so you can set IsTabStop property to falseto make buttons cannot receive input focus.

If IsTabStop is false, the control is excluded from tab navigation. In addition, if IsTabStop is false, the control cannot receive input focus. (If you try to set focus programmatically, by calling the Focus method, Focus returns false).

So you can change your XAML code like:

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

After this, the buttons won't get foucs and if your textbox has got focus, it will keep focus when users click or tap on the buttons.