Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 对话框碎片宽度和高度被忽略_C#_Android_Xamarin_Visual Studio 2017 - Fatal编程技术网

C# 对话框碎片宽度和高度被忽略

C# 对话框碎片宽度和高度被忽略,c#,android,xamarin,visual-studio-2017,C#,Android,Xamarin,Visual Studio 2017,如何设置对话框片段的宽度和高度?我读到的所有内容都说要覆盖OnStart方法,如下所示: public override void OnStart() { if (Dialog == null) { return; } int dialogWidth = 300; int dialogHeight = 75; Dialog.Window.SetLayout(di

如何设置对话框片段的宽度和高度?我读到的所有内容都说要覆盖OnStart方法,如下所示:

    public override void OnStart()
    {
        if (Dialog == null)
        {
            return;
        }
        int dialogWidth = 300;
        int dialogHeight = 75;
        Dialog.Window.SetLayout(dialogWidth, dialogHeight);
    }
当我这样做时,应用程序崩溃,我无法捕获错误,因为它显然在我编写的代码之外失败。如果没有此替代,对话框将显示并按预期操作

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    android:minWidth="300dp" 
    android:minHeight="75dp">
    <TextView
        android:text="Select section."
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center" 
        android:textStyle="bold"
        android:id="@+id/textView2" />
    <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:id="@+id/my_spinner" />
    <Button
        android:text="Ok"
        android:layout_width="200px"
        android:layout_gravity="center"
        android:layout_height="34.5dp"
        android:id="@+id/button_ok" />
</LinearLayout>

android:minWidth=“300dp”
android:minHeight=“75dp”>

显然,
android:minWidth
android:minHeight
设置被忽略,因为我得到的只是一个很小的对话框。

最好在
values
文件夹下的
dimens.xml
中定义大小:

<resources>
    ...
    <dimen name="dialog_min_width">300dp</dimen>
    <dimen name="dialog_min_height">75dp</dimen>
    ...
</resources>
Mehmed代码的c#等价物:

    public override void OnResume()
    {
        int width = 300; 
        int height = 75; 
        base.OnResume();
    }
我发现Xamarin关注的堆栈溢出上发布的许多代码都是java代码,我使用c#(Visual Studio 2017),对java一无所知,很难转换为c#,令人沮丧。当我在答案中看到java代码时,我通常直接跳过它。但是,看到我是如何提出这个问题的,而且迈赫迈德很友好地提供了一个有效的解决方案,我觉得有必要发布一个c#conversion

您会注意到,我选择不将值放在
dimens.xml
中,我不喜欢将值存储在“代码文件外部”中,然后在代码中检索它们,这有时会使调试变得更加困难。当你这样做的时候,编译器会用生成的obj文件soooo中的值替换代码行。。为什么这样做


考虑到这些值永远不会改变,我认为将它们放在
dimens.xml
中没有任何意义,请务必说服我不要这样做

我将您的代码从java转换为c#,并将其添加到您的答案中。这确实有效,尽管我不明白为什么这在
OnResume
中有效,而在
OnStart
中无效。旁注:我从来没有看到将值存储在外部文件中,然后重新编译以供以后使用有什么好处。我想我有点老派了。哈哈…@PrescottChartier:我投票否决了你的编辑,但我很感激。作者选择提供一个Java答案,不管是正确的还是错误的,并且它可以根据自己的优点。在此期间,您可以编写自己的答案,如果您希望得到Mehmed的答案,很抱歉用Java提供解决方案,但我知道它可以移植到Xamarin。此外,这是一种惯例,我使用dimensions文件提供dp中的大小,并使用
getDimensionPixelSize()
获取像素中的大小,以避免手动计算。
    public override void OnResume()
    {
        int width = 300; 
        int height = 75; 
        base.OnResume();
    }