2016-05-17 51 views
0

我有一个UWP应用程序,它有一系列带有点击事件的按钮。我也有一个文本框,我需要专注于应用程序的几乎每次点击/触摸。所以我设置了这样的resetFocus功能:获取lostfocus事件上的点击按钮

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事件发生在点击和点击功能从未触发之前。

UPDATE: 这是从按钮创建对话框代码:

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&&@[email protected]&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); 
    } 
+0

不知道UWP是否也有这个功能,但在常规的WPF中,我只是在按钮上设置了'Focusable =“False”'。这样你的文本框就不会失去焦点。 –

回答

0

看来你想,当你点击或点击按钮,按钮不会得到焦点。如果是这样,您可以将IsTabStop property设置为false以使按钮无法接收输入焦点。

如果IsTabStop,控制从标签导航排除。另外,如果IsTabStopfalse,控件无法接收输入焦点。 (如果尝试以编程方式设置焦点,请通过调用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"> 

在此之后,该按钮不会得到FOUCS,如果你的文本得到了焦点,它会保持专注当用户点击或点击按钮。

+0

完美。正是我在找什么! – Janinho67