简体   繁体   English

在lostfocus事件上获得点击按钮

[英]Get clicked button on lostfocus event

I have an UWP application which have a series of buttons with a click event. 我有一个UWP应用程序,该应用程序具有一系列带有单击事件的按钮。 I also have a textbox that I need to keep focus on almost every click/touch on the app. 我还有一个文本框,我需要将注意力集中在应用程序的几乎每次单击/触摸上。 So I set a resetFocus function like this: 所以我像这样设置一个resetFocus函数:

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

that triggers on every click outside this textbox. 触发此文本框之外的每次点击。

This is the button in the XAML 这是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>

My Question is: 我的问题是:

How can I call the function normally called with the button click inside of this function ? 我如何才能调用通常在该函数内部单击按钮调用的函数?

Since the lostfocus event happens before the click and the click function is never triggered. 由于lostfocus事件发生在点击之前,因此永远不会触发点击功能。

UPDATE: This is the code that create the dialog from the button: 更新:这是从按钮创建对话框的代码:

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);
    }

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 false to make buttons cannot receive input focus. 如果是这样,则可以将IsTabStop属性设置为false以使按钮无法接收输入焦点。

If IsTabStop is false , the control is excluded from tab navigation. 如果IsTabStopfalse ,则该控件将从选项卡导航中排除。 In addition, if IsTabStop is false , the control cannot receive input focus. 此外,如果IsTabStopfalse ,则控件无法接收输入焦点。 (If you try to set focus programmatically, by calling the Focus method, Focus returns false ). (如果尝试通过编程方式设置焦点,则通过调用Focus方法, Focus返回false )。

So you can change your XAML code like: 因此,您可以像这样更改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">

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. 此后,按钮将不会出现焦点,并且如果您的文本框已获得焦点,则当用户单击或点击按钮时,它将保持焦点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM