2011-04-05 101 views
1

Microsoft的unit converter starter kit爲其Windows 7手機生成帶有XAML的鍵盤,如下所示。顯然噸複製和粘貼。使用XAML時避免複製和粘貼

問題:

1)MSFT是否認真寫這種方式?
2)真的嗎?
3)有沒有一種健全的方式來寫這樣的東西?
4)有誰知道代碼示例說明了這種理智的方式嗎?

<StackPanel Orientation="Vertical"> 
     <TextBlock x:Name="textBlockCategory" 
        Text="{Binding Path=UppercasedCategory}" 
        Margin="23,24,0,0" 
        FontSize="{StaticResource PhoneFontSizeMedium}" 
        FontFamily="{StaticResource PhoneFontFamilyNormal}" 
        Foreground="{StaticResource PhoneForegroundBrush}" /> 
     <StackPanel x:Name="spInput" 
        Orientation="Vertical" 
        Margin="0,0,24,0" > 
      <TextBlock x:Name="textBlockInputValue" 
         Text="{Binding Path=UpperUnitValue}" 
         Height="80" Margin="0,2,0,0" 
         TextAlignment="Right" 
         FontSize="{StaticResource PhoneFontSizeExtraExtraLarge}" 
         FontFamily="{StaticResource PhoneFontFamilyNormal}" 
         Foreground="{StaticResource PhoneForegroundBrush}" /> 
      <TextBlock x:Name="textBlockInputUnit" 
         Text="{Binding Path=UpperUnitName}" 
         Height="34" TextAlignment="Right" Margin="0,2,0,0" 
         FontSize="{StaticResource PhoneFontSizeMediumLarge}" 
         Foreground="{StaticResource PhoneAccentBrush}" 
         FontFamily="{StaticResource PhoneFontFamilySemiBold}" /> 
     </StackPanel> 
     <StackPanel x:Name="spResult" 
        Orientation="Vertical" 
        Margin="0,-4,24,0" > 
      <TextBlock x:Name="textBlockResultValue" 
         Text="{Binding Path=LowerUnitValue}" 
         Height="80" TextAlignment="Right" Margin="0,-4,0,0" 
         FontSize="{StaticResource PhoneFontSizeExtraExtraLarge}" 
         FontFamily="{StaticResource PhoneFontFamilyNormal}" 
         Foreground="{StaticResource PhoneSubtleBrush}"/> 
      <TextBlock x:Name="textBlockResultUnit" 
         Text="{Binding Path=LowerUnitName}" 
         Height="34" TextAlignment="Right" Margin="0,2,0,0" 
         FontSize="{StaticResource PhoneFontSizeMediumLarge}" 
         Foreground="{StaticResource PhoneAccentBrush}" 
         FontFamily="{StaticResource PhoneFontFamilySemiBold}" /> 
     </StackPanel> 
     <StackPanel x:Name="spSummary" > 
      <TextBlock x:Name="textBlockSummary" 
         Text="{Binding Path=Summary}" 
         Height="30" TextAlignment="Center" Margin="0,18,0,0" 
         FontSize="{StaticResource PhoneFontSizeNormal}" 
         Foreground="{StaticResource PhoneSubtleBrush}" 
         FontFamily="{StaticResource PhoneFontFamilyNormal}" /> 
     </StackPanel> 
     <StackPanel x:Name="spKeypad" Orientation="Vertical" HorizontalAlignment="Right"> 
      <StackPanel x:Name="spKeypadRow1" Orientation="Horizontal"> 
       <Button x:Name="btnKeypadKey7" 
         Content="7" 
         Background="{StaticResource PhoneInactiveBrush}" 
         Foreground="{StaticResource PhoneForegroundBrush}" 
         Height="84" Margin="6" Width="108" 
         Style="{StaticResource KeypadButtonStyle}" 
         FontFamily="{StaticResource PhoneFontFamilyNormal}" 
         FontSize="{StaticResource KeypadButtonFontSize}" 
         Click="OnClickNumber" /> 
       <Button x:Name="btnKeypadKey8" 
         Content="8" 
         Background="{StaticResource PhoneInactiveBrush}" 
         Foreground="{StaticResource PhoneForegroundBrush}" 
         Height="84" Margin="6" Width="108" 
         Style="{StaticResource KeypadButtonStyle}" 
         FontFamily="{StaticResource PhoneFontFamilyNormal}" 
         FontSize="{StaticResource KeypadButtonFontSize}" 
         Click="OnClickNumber" /> 
       <Button x:Name="btnKeypadKey9" 
         Content="9" 
         Background="{StaticResource PhoneInactiveBrush}" 
         Foreground="{StaticResource PhoneForegroundBrush}" 
         Height="84" Margin="6" Width="108" 
         Style="{StaticResource KeypadButtonStyle}" 
         FontFamily="{StaticResource PhoneFontFamilyNormal}" 
         FontSize="{StaticResource KeypadButtonFontSize}" 
         Click="OnClickNumber" /> 
       <Button x:Name="btnKeypadKeyCancel" 
         Height="84" Margin="6" Width="108" 
         Style="{StaticResource btnKeypadKeyCancelStyle}" 
         Background="{StaticResource PhoneDisabledBrush}" 
         FontSize="{StaticResource PhoneFontSizeExtraLarge}" 
         FontFamily="{StaticResource PhoneFontFamilyNormal}" 
         Click="OnClickClear" /> 
      </StackPanel> 
      <StackPanel x:Name="spKeypadRow2" Orientation="Horizontal"> 
       <Button x:Name="btnKeypadKey4" 
         Content="4" 
         Background="{StaticResource PhoneInactiveBrush}"  
         Foreground="{StaticResource PhoneForegroundBrush}"   
         Height="84" Margin="6" Width="108" 
         Style="{StaticResource KeypadButtonStyle}" 
         FontSize="{StaticResource KeypadButtonFontSize}" 
         FontFamily="{StaticResource PhoneFontFamilyNormal}" 
         Click="OnClickNumber" /> 
       <Button x:Name="btnKeypadKey5" 
         Content="5" 
         Background="{StaticResource PhoneInactiveBrush}"  
         Foreground="{StaticResource PhoneForegroundBrush}"   
         Height="84" Margin="6" Width="108" 
         Style="{StaticResource KeypadButtonStyle}" 
         FontSize="{StaticResource KeypadButtonFontSize}" 
         FontFamily="{StaticResource PhoneFontFamilyNormal}" 
         Click="OnClickNumber" /> 
       <Button x:Name="btnKeypadKey6" 
         Content="6" 
         Background="{StaticResource PhoneInactiveBrush}"  
         Foreground="{StaticResource PhoneForegroundBrush}"   
         Height="84" Margin="6" Width="108" 
         Style="{StaticResource KeypadButtonStyle}" 
         FontSize="{StaticResource KeypadButtonFontSize}" 
         FontFamily="{StaticResource PhoneFontFamilyNormal}" 
         Click="OnClickNumber" /> 
       <Button x:Name="btnKeypadKeyBack" 
         Height="84" Margin="6" Width="108" 
         Style="{StaticResource btnKeypadKeyBackStyle}" 
         Background="{StaticResource PhoneDisabledBrush}" 
         FontSize="{StaticResource PhoneFontSizeExtraLarge}" 
         FontFamily="{StaticResource PhoneFontFamilyNormal}" 
         Click="OnClickBack" /> 
      </StackPanel> 
      <StackPanel x:Name="spKeypadRow3" Orientation="Horizontal"> 
       <Button x:Name="btnKeypadKey1"  
         Background="{StaticResource PhoneInactiveBrush}"  
         Foreground="{StaticResource PhoneForegroundBrush}"  
         Height="84" Margin="6" Width="108" 
         Content="1" 
         Style="{StaticResource KeypadButtonStyle}" 
         FontSize="{StaticResource KeypadButtonFontSize}" 
         FontFamily="{StaticResource PhoneFontFamilyNormal}" 
         Click="OnClickNumber" /> 
       <Button x:Name="btnKeypadKey2"  
         Content="2" 
         Background="{StaticResource PhoneInactiveBrush}"  
         Foreground="{StaticResource PhoneForegroundBrush}"  
         Height="84" Margin="6" Width="108" 
         Style="{StaticResource KeypadButtonStyle}" 
         FontSize="{StaticResource KeypadButtonFontSize}" 
         FontFamily="{StaticResource PhoneFontFamilyNormal}" 
         Click="OnClickNumber" /> 
       <Button x:Name="btnKeypadKey3"  
         Content="3" 
         Background="{StaticResource PhoneInactiveBrush}"  
         Foreground="{StaticResource PhoneForegroundBrush}"  
         Height="84" Margin="6" Width="108" 
         Style="{StaticResource KeypadButtonStyle}" 
         FontSize="{StaticResource KeypadButtonFontSize}" 
         FontFamily="{StaticResource PhoneFontFamilyNormal}" 
         Click="OnClickNumber" /> 
       <Button x:Name="btnKeypadKeyPlusMinus" 
         Height="84" Margin="6" Width="108" 
         Style="{StaticResource btnKeypadPlusMinusStyle}" 
         Background="{StaticResource PhoneDisabledBrush}" 
         FontSize="{StaticResource PhoneFontSizeExtraLarge}" 
         FontFamily="{StaticResource PhoneFontFamilyNormal}" 
         Click="OnClickSign" /> 
      </StackPanel> 
      <StackPanel x:Name="spKeypadRow4" Orientation="Horizontal"> 
       <Button x:Name="btnKeypadKey0" 
         Content="0" 
         Background="{StaticResource PhoneInactiveBrush}"  
         Foreground="{StaticResource PhoneForegroundBrush}" 
         Height="84" Margin="6" Width="228" 
         Style="{StaticResource KeypadButtonStyle}" 
         FontSize="{StaticResource KeypadButtonFontSize}" 
         FontFamily="{StaticResource PhoneFontFamilyNormal}" 
         Click="OnClickNumber" /> 
       <Button x:Name="btnKeypadKeyPoint" 
         Content="{Binding Path=CurrentCulture.NumberFormat.NumberDecimalSeparator, Source={StaticResource Resources}}" 
         Background="{StaticResource PhoneInactiveBrush}" 
         Foreground="{StaticResource PhoneForegroundBrush}" 
         Height="84" Margin="6" Width="108" 
         Style="{StaticResource btnKeypadPointStyle}" 
         FontSize="{StaticResource KeypadButtonFontSize}" 
         FontFamily="{StaticResource PhoneFontFamilyNormal}" 
         Click="OnClickNumber" /> 
       <Button x:Name="btnKeypadSwitchSourceTargetUnit" 
         Height="84" Margin="6" Width="108" 
         Style="{StaticResource KeypadButtonStyle}" 
         Background="{StaticResource PhoneDisabledBrush}" 
         FontSize="{StaticResource PhoneFontSizeExtraLarge}" 
         FontFamily="{StaticResource PhoneFontFamilyNormal}" 
         Click="OnClickSourceTargetUnit"> 
        <Image Source= "{Binding ConversionImageSource}"/> 
       </Button> 
      </StackPanel> 
     </StackPanel> 
    </StackPanel> 
+0

你想達到什麼目的? XAML看起來像一個自定義鍵盤,而不是內置的鍵盤。除了在內置鍵盤和輸入範圍之間進行選擇外,這將是獲得完整定製鍵盤的唯一方法。 – 2011-04-05 02:19:21

+0

我想製作自定義鍵盤而不使用複製和粘貼嘉豪。恐怕我是一個XAML newb,但在其他語言中,我會使用一些循環來避免重複使用相同的代碼,並且一次又一次地重複更改。 – 2011-04-05 02:22:56

+0

您使用設計器來生成這種措詞。 VS或Expression Blend中的WPF設計器。或者你僱用的人爲你做,所以看起來不錯。 – 2011-04-05 02:41:17

回答

1

如果你要模仿相同的設計,我肯定會利用樣式來減少所有這些按鈕的重複。看起來他們已經通過創建KeypadButtonStyle開始了這個過程,但是您可以創建更多,特別是如果您使用基於其他樣式的樣式。

您也可以使用網格而不是堆疊面板堆疊面板進行佈局。

就構建xaml代碼和循環而言,你可以做到這一點,但我強烈建議學習Expression Blend,它不需要很長時間就可以創建所需的xaml(包括樣式優化)。

0

不,您不必複製和粘貼XAML代碼,如果你不想。 XAML只是一個創建對象並設置屬性的對象圖。您在XAML中編寫的任何代碼都可以通過程序編寫(如果需要,可以在for循環中)。

1

XAML是一種非常強大的語言,但正如您正確地指出的那樣,它本質上是冗長和醜陋的。這是我乾的最糟糕的語言(不要重複自己)。也就是說,其他人所做的評論也是如此。樣式幫助了很多。混合幫助更多。

我的第一印象和你的第一印象是一樣的,儘管後來我意識到它確實是一種非常強大和有用的語言,但我從未停止過認爲其中的一部分設計得非常糟糕。這是一個例子;你不能指定你要數據綁定的對象的數據類型是另一個事實。

所有這一切說,真的值得學習XAML。您可以使用XAML來完成這些在HTML或WinForms或Java中完全不可能的血腥事件。這是一個非常非常強大的UI描述語言。只要掌握它的內容,努力學習,忘記你所學到的關於良好編碼風格的所有內容,就可以創建一些令人驚歎的UI。