[英]How to display a BoxView using C# passing parameters of type Double?
I've created a Xamarin.Forms project with both a XAML layout and a C# layout to be proficient on both sides of the coin.
在我的 Xaml ContentPage 上,我在 StackLayout 中做了一個 Boxview,如下所示
<ScrollView>
<StackLayout>
<BoxView HeightRequest="100"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand"
WidthRequest="100">
<BoxView.Color>
<Color x:FactoryMethod="FromRgb">
<x:Arguments>
<x:Int32>0</x:Int32>
<x:Int32>0</x:Int32>
<x:Int32>255</x:Int32>
</x:Arguments>
</Color>
</BoxView.Color>
</BoxView>
<StackLayout>
</ScrollView>
在我的 C# ContentPage 上復制它
this.Content = new ScrollView {
Content = new StackLayout {
Children = {
new BoxView{
Color = Color.FromRgb(0, 0, 255),
HeightRequest = 100,
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.CenterAndExpand,
WidthRequest = 100
}
}
};
}
在兩頁上,我都得到了藍色框的正確結果
我在使用 Double 類型參數在 C# 中復制以下內容時遇到問題
<Color x:FactoryMethod="FromRgb">
<x:Arguments>
<x:Double>1</x:Double>
<x:Double>0</x:Double>
<x:Double>0</x:Double>
</x:Arguments>
</Color>
使用構造函數; public Color (double r, double g, double b);
var myColor = new Color(r,g,b);
從這篇Color.FromRgb Method文章中,我們可以看到有兩種方法,具有 int 和 double 參數。
所以如果你使用Double類型的參數,你可以在C#中也需要使用Double類型的參數。
this.Content = new StackLayout
{
Children = {
new BoxView{
Color = Color.FromRgb((double)1,(double)0, (double)0),
HeightRequest = 100,
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.CenterAndExpand, WidthRequest = 100
}
}
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.