在 lostfocus 事件上获取点击按钮
Get clicked button on lostfocus event
我有一个 UWP 应用程序,它有一系列带有点击事件的按钮。我还有一个文本框,我需要将注意力集中在应用程序上的几乎每个 click/touch 上。所以我像这样设置了一个 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);
}
您似乎希望在单击或点击按钮时按钮不会获得焦点。如果是这样,您可以将 IsTabStop
property 设置为 false
以使按钮无法接收输入焦点。
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).
所以您可以像这样更改 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">
在此之后,按钮将不会获得焦点,如果您的文本框获得焦点,当用户单击或点击按钮时它将保持焦点。
我有一个 UWP 应用程序,它有一系列带有点击事件的按钮。我还有一个文本框,我需要将注意力集中在应用程序上的几乎每个 click/touch 上。所以我像这样设置了一个 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);
}
您似乎希望在单击或点击按钮时按钮不会获得焦点。如果是这样,您可以将 IsTabStop
property 设置为 false
以使按钮无法接收输入焦点。
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).
所以您可以像这样更改 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">
在此之后,按钮将不会获得焦点,如果您的文本框获得焦点,当用户单击或点击按钮时它将保持焦点。