2016-05-17 50 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