在 WP7 中更改用户控件(进度条)的方向?

发布于 2024-12-16 23:43:48 字数 1443 浏览 0 评论 0原文

我在我的应用程序中使用进度条,该进度条是在用户控件内部定义的,例如:

UserControl x:Class="StirLibrary.ProgressBarControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
mc:Ignorable="d"  d:DesignHeight="800" d:DesignWidth="480">

<Grid x:Name="LayoutRoot" Height="800">
    <Border BorderThickness="2" BorderBrush="Transparent" Background="Transparent" Margin="50,522,50,158">
        <StackPanel>
            <TextBlock Text="Loading..." Name="loading" Grid.Row="1" HorizontalAlignment="Center" Height="30" Foreground="Green">
            </TextBlock>
            <ProgressBar Background="Transparent" Margin="10, 0, 0, 10" Height="80" HorizontalAlignment="Center" Name="progressBar1" VerticalAlignment="Top" Width="380" Grid.Row="2" HorizontalContentAlignment="Left" IsHitTestVisible="True" VerticalContentAlignment="Top" Value="0" Maximum="100">
            </ProgressBar>
        </StackPanel>
    </Border>
</Grid>
</UserControl>

我的问题是当我的应用程序的方向更改为横向时,进度条的方向不会改变,这使得应用程序看起来很难看。欢迎提出如何避免这种情况并使进度条按方向显示的任何建议。

I'm using a progress bar in my app, this progress bar is defined inside the user control, e.g.:

UserControl x:Class="StirLibrary.ProgressBarControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
mc:Ignorable="d"  d:DesignHeight="800" d:DesignWidth="480">

<Grid x:Name="LayoutRoot" Height="800">
    <Border BorderThickness="2" BorderBrush="Transparent" Background="Transparent" Margin="50,522,50,158">
        <StackPanel>
            <TextBlock Text="Loading..." Name="loading" Grid.Row="1" HorizontalAlignment="Center" Height="30" Foreground="Green">
            </TextBlock>
            <ProgressBar Background="Transparent" Margin="10, 0, 0, 10" Height="80" HorizontalAlignment="Center" Name="progressBar1" VerticalAlignment="Top" Width="380" Grid.Row="2" HorizontalContentAlignment="Left" IsHitTestVisible="True" VerticalContentAlignment="Top" Value="0" Maximum="100">
            </ProgressBar>
        </StackPanel>
    </Border>
</Grid>
</UserControl>

My problem is when the orientation of my app changes to landscape the progress bar's orientation doesn't change and this makes the app look ugly. Any suggestions how to avoid this and make the progress bar displayed as per orientation are welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦里的微风 2024-12-23 23:43:48

正如马特上面提到的,不可能在用户控件中定向弹出窗口,因为用户控件没有任何支持方向的空间。但由于这对我们的应用程序来说是非常关键的要求,我找到了一个解决方法,并对主页的类文件和用户控件的类文件进行了一些更改。更改是:

 private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
        {
            if ((e.Orientation & PageOrientation.Portrait) == PageOrientation.Portrait)
            {
ProgressBarControl.getInstance().ProgressBarControl_LayoutUpdated(this, e,e.Orientation.ToString());
}
else if ((e.Orientation & PageOrientation.Landscape) == PageOrientation.Landscape)
            {
ProgressBarControl.getInstance().ProgressBarControl_LayoutUpdated(this, e, e.Orientation.ToString());
}
}

这些是 MainPage.xaml.cs 中的更改

public partial class ProgressBarControl : UserControl
{
    private static ProgressBarControl instance = null;
    public static Popup popup;

    private ProgressBarControl()
    {
        InitializeComponent();
    }
    public static ProgressBarControl getInstance()
    {
        if (instance == null)
        {
            instance = new ProgressBarControl();
            popup = new Popup();
            popup.Child = instance;
            popup.IsOpen = false;
        }
        return instance;
    }
    public void ProgressBarControl_LayoutUpdated(object sender, EventArgs e,string orientation)
    {
        if (orientation == "LandscapeRight")
        {
            ProgressPanel.RenderTransformOrigin = new Point(0.5, 0.5);
            ProgressPanel.RenderTransform = new CompositeTransform { Rotation = 270 };
        }
        else if(orientation == "LandscapeLeft")
        {
            ProgressPanel.RenderTransformOrigin = new Point(0.5, 0.5);
            ProgressPanel.RenderTransform = new CompositeTransform { Rotation = 90 };
        }
        else
        {
            ProgressPanel.RenderTransformOrigin = new Point(0, 0);
            ProgressPanel.RenderTransform = new CompositeTransform { Rotation = 0 };
        }

    }

    public static void displayProgressBar(int requestId, int status, string msg)
    {
        System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                if (instance == null)
                {
                    instance = new ProgressBarControl();
                    popup = new Popup();
                    popup.Child = instance;
                }
                popup.IsOpen = true;
                instance.loading.Text = msg;
                instance.progressBar1.IsIndeterminate = true;
                instance.progressBar1.Value = status;
            });
    }
    public static void dismissProgressBar()
    {
        System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                if(popup!=null)
                {
                    popup.IsOpen = false;
                }
            });
    }
}

,这是什么我已经在我的 ProgressBarControl.cs 文件(这是用户控件的类文件)

Xaml 文件中完成了:

<UserControl x:Class="StirLibrary.ProgressBarControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
mc:Ignorable="d"  d:DesignHeight="800" d:DesignWidth="480">

<Grid x:Name="LayoutRoot" Height="800">
    <!--<Border BorderThickness="2" BorderBrush="Black" Background="Transparent" Margin="54,406,50,320"></Border>-->
    <StackPanel x:Name="ProgressPanel" Background="Black" Margin="54,406,50,320">
        <TextBlock Text="Loading..." Name="loading" Grid.Row="1" HorizontalAlignment="Center" Height="32" Foreground="White"></TextBlock>
        <ProgressBar Background="Green" Margin="10, 0, 0, 10" Height="33" Foreground="White" HorizontalAlignment="Center" Name="progressBar1" VerticalAlignment="Top" Width="351" Grid.Row="2" HorizontalContentAlignment="Left" IsHitTestVisible="True" VerticalContentAlignment="Top" Value="0" Maximum="100"></ProgressBar>
    </StackPanel>
</Grid>
</UserControl>

As Matt has mentioned above it is not possible to orient a pop up in user control because User control doesn't have any room for supported orientation. but since it was very crucial requirement for our App i found a work around and made few changes in the Main Page's class file and the user control's class file.. the changes are:

 private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
        {
            if ((e.Orientation & PageOrientation.Portrait) == PageOrientation.Portrait)
            {
ProgressBarControl.getInstance().ProgressBarControl_LayoutUpdated(this, e,e.Orientation.ToString());
}
else if ((e.Orientation & PageOrientation.Landscape) == PageOrientation.Landscape)
            {
ProgressBarControl.getInstance().ProgressBarControl_LayoutUpdated(this, e, e.Orientation.ToString());
}
}

These are the changes in MainPage.xaml.cs

public partial class ProgressBarControl : UserControl
{
    private static ProgressBarControl instance = null;
    public static Popup popup;

    private ProgressBarControl()
    {
        InitializeComponent();
    }
    public static ProgressBarControl getInstance()
    {
        if (instance == null)
        {
            instance = new ProgressBarControl();
            popup = new Popup();
            popup.Child = instance;
            popup.IsOpen = false;
        }
        return instance;
    }
    public void ProgressBarControl_LayoutUpdated(object sender, EventArgs e,string orientation)
    {
        if (orientation == "LandscapeRight")
        {
            ProgressPanel.RenderTransformOrigin = new Point(0.5, 0.5);
            ProgressPanel.RenderTransform = new CompositeTransform { Rotation = 270 };
        }
        else if(orientation == "LandscapeLeft")
        {
            ProgressPanel.RenderTransformOrigin = new Point(0.5, 0.5);
            ProgressPanel.RenderTransform = new CompositeTransform { Rotation = 90 };
        }
        else
        {
            ProgressPanel.RenderTransformOrigin = new Point(0, 0);
            ProgressPanel.RenderTransform = new CompositeTransform { Rotation = 0 };
        }

    }

    public static void displayProgressBar(int requestId, int status, string msg)
    {
        System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                if (instance == null)
                {
                    instance = new ProgressBarControl();
                    popup = new Popup();
                    popup.Child = instance;
                }
                popup.IsOpen = true;
                instance.loading.Text = msg;
                instance.progressBar1.IsIndeterminate = true;
                instance.progressBar1.Value = status;
            });
    }
    public static void dismissProgressBar()
    {
        System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                if(popup!=null)
                {
                    popup.IsOpen = false;
                }
            });
    }
}

and this what i have done in my ProgressBarControl.cs file (this is the user control's class file)

Xaml file:

<UserControl x:Class="StirLibrary.ProgressBarControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
mc:Ignorable="d"  d:DesignHeight="800" d:DesignWidth="480">

<Grid x:Name="LayoutRoot" Height="800">
    <!--<Border BorderThickness="2" BorderBrush="Black" Background="Transparent" Margin="54,406,50,320"></Border>-->
    <StackPanel x:Name="ProgressPanel" Background="Black" Margin="54,406,50,320">
        <TextBlock Text="Loading..." Name="loading" Grid.Row="1" HorizontalAlignment="Center" Height="32" Foreground="White"></TextBlock>
        <ProgressBar Background="Green" Margin="10, 0, 0, 10" Height="33" Foreground="White" HorizontalAlignment="Center" Name="progressBar1" VerticalAlignment="Top" Width="351" Grid.Row="2" HorizontalContentAlignment="Left" IsHitTestVisible="True" VerticalContentAlignment="Top" Value="0" Maximum="100"></ProgressBar>
    </StackPanel>
</Grid>
</UserControl>
我是男神闪亮亮 2024-12-23 23:43:48

我可以通过简单地添加到主屏幕的子项(在其顶部显示弹出窗口)来启用弹出用户控件的方向:

popUp = new Popup();
loginControl = new LoginPopup();  // this is the custom UserControl

popUp.Child = loginControl;
LayoutRoot.Children.Add(popUp);

I could enable the orientation for my popup UserControl by simply adding to the Children of the main screen on top of which the Popup is being displayed as:

popUp = new Popup();
loginControl = new LoginPopup();  // this is the custom UserControl

popUp.Child = loginControl;
LayoutRoot.Children.Add(popUp);
世俗缘 2024-12-23 23:43:48

Popup 类不支持方向,因此您不能使用它并期望它处理方向更改。这与弹出窗口中显示的控件是否位于同一程序集中无关。

除了使用 Popup,一个简单的替代方案是将控件直接放在页面上所有其他内容的顶部。如果您愿意,可以将其包含在另一个控件(例如网格或面板)中。

手动向控件添加 RotateTransform 将使您能够添加额外的控制来调整方向,但如果可以避免的话,我建议不要走这条路。

The Popup class does not support orientation so you can't use this and expect it to handle orientation changes. This is regardless of whether the control displayed in the popup is in the same assembly or not.

Instead of using a Popup a simple alternative would be to put the control directly on top of all other content on the page. You could include this inside another control (such as a grid or a panel) if you wish.

Manually adding a RotateTransform to the control will give you the ability to add extra control over adjusting the orientation but I'd recommend not going down this route if you can avoid it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文