如何在WindowsPhone8中将xaml代码转换为c#代码

如何在WindowsPhone8中将xaml代码转换为c#代码,c#,xaml,windows-phone-8,C#,Xaml,Windows Phone 8,如何将此xaml代码转换为cs: <StackPanel x:Name="mainsp"> <Border Style="{StaticResource bordercss}" > <StackPanel x:Name="getme" Background="White" Orientation="Vertical" d:DataContext="{Binding TaskList[0]}" > <

如何将此xaml代码转换为cs:

<StackPanel x:Name="mainsp">    
    <Border Style="{StaticResource bordercss}" >
        <StackPanel x:Name="getme"  Background="White" Orientation="Vertical" d:DataContext="{Binding TaskList[0]}" >
            <TextBlock Text="{Binding title}" Style="{StaticResource btbx_bold}" />
            <TextBlock Text="{Binding csoId}" Style="{StaticResource btbx_new}" />
            <TextBlock  Style="{StaticResource btbx_new}" >
                <Run Text="Date : " />
                <Run Text="{Binding mydate}" />
            </TextBlock>
            <TextBlock Style="{StaticResource btbx_new}"  >
                <Run Text="Status : " />
                <Run Text="{Binding mystatus}" />
            </TextBlock>
            <TextBlock Style="{StaticResource btbx_new}" >
                <Run Text="Customer Info : " />
                <Run Text="{Binding clientname}" />                        
                <Run Text=" ("/>                    
                <Run Text="{Binding customercontact}" />
                <Run Text=") "/>
            </TextBlock>
            <StackPanel Orientation="Horizontal">
                <Image Source="/Assets/address_icon.png" Margin="5,0"/>
                <TextBlock Text="{Binding csoId}" Style="{StaticResource btbx_new}" />
            </StackPanel>
            <Line StrokeThickness="2" Fill="Black" Margin="5,0" />
            <Button Content="Get Direction" Background="#B80E0F" Click="Button_Click_1"  FontSize="18"/>
        </StackPanel>
    </Border>
</StackPanel>

我不会转换整个代码,因为这需要一些时间,但您可以在MSDN上找到所有内容。以下是一个例子:

var stackPanel = new StackPanel();
var border = new Border();
stackPanel.Children.Add(border);

var stackPanel2 = new StackPanel();
border.Content = stackPanel;
等等

关于绑定,请参见。样本:

就是这样

StackPanel mainsp=new StackPanel() //main parent

Border bb=new Border()
bb.Style=(Style)App.Current.Resources["bordercss"];
mainsp.Children.Add(bb);

StackPanel getme=new StackPanel();

TextBlock textblock=new TextBlock();
Run myRun=new Run();
myRun.text="Hi";
textblock.inlines.Add(myRun);
getme.Children.Add(textblock);

等等。

你需要知道的一切都在

基本原则是:

每个标记名对应一个类名,通常位于
System.Windows.Controls
命名空间下,每个属性对应一个属性(您可能已经知道这一点)

嵌套元素(除非它们被包装在专门指向属性(例如
Grid.RowDefinitions
)的标记中)被指定给类上为包含标记指定的属性。如果它是集合类型,则每个嵌套元素都将添加到其中。例如,在的声明中,您可以看到
[ContentPropertyAttribute(“Inlines”)]
,因此包含
文本块运行时将转换为:

var tb = new TextBlock();
var run1 = new Run();
var run2 = new Run();
tb.Inlines.Add(run1);
tb.Inlines.Add(run2);
获取元素作用域中资源键的值。在初始化元素时调用此函数相当于
StaticResource
。对于
DynamicResource
,请使用

将绑定应用于属性

因此,上述
TextBlock
的完整翻译为:

var tb = new TextBlock();
tb.Style = (Style)tb.FindResource("btbx_new");
var run1 = new Run {Text = "Date : "};
var run2 = new Run();
run2.SetBinding(Run.TextProperty, "mydate");
tb.Inlines.Add(run1);
tb.Inlines.Add(run2);

检查这些答案,它们对我有用:///为什么不在windows Phone中使用XAML?据我所知,您的代码应该可以正常工作,无需任何更改。我在地图的toottip中未使用此选项,例如在pushpinno bro中绑定此数据。我在数据绑定方面有问题。有关详细信息,请参阅我的编辑。
var tb = new TextBlock();
tb.Style = (Style)tb.FindResource("btbx_new");
var run1 = new Run {Text = "Date : "};
var run2 = new Run();
run2.SetBinding(Run.TextProperty, "mydate");
tb.Inlines.Add(run1);
tb.Inlines.Add(run2);