【问题标题】:Changing the orientation of a user control (ProgressBar) in WP7?在 WP7 中更改用户控件(ProgressBar)的方向?
【发布时间】:2011-11-18 16:28:36
【问题描述】:

我在我的应用程序中使用了一个进度条,这个进度条是在用户控件中定义的,例如:

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>

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

【问题讨论】:

  • 如何显示用户控件?如果它在弹出窗口中,则这是弹出窗口的已知问题。
  • 我知道这不能回答您的问题,但也许您会考虑使用由操作系统处理的 ProgressIndicator,它是显示进度的标准:msdn.microsoft.com/en-us/library/…。否则,我还会考虑查看 Silverlight 工具包中的 PerformanceProgressBar,据说它在某些方面比开箱即用的 ProgressBar 更好。
  • @Matt Am 在弹出窗口中显示用户控件。我的问题是我的整个解决方案有 3 个项目 UI 部分、库部分和调度程序部分。这个进度条控件在库部分和我无法在我的 UI 部分中访问它,我在其中调用事件以更改方向。我知道我的问题的部分解决方案..即,包含进度条的堆栈面板必须旋转 90 度并且(渲染变换原点)当方向改变时必须为堆栈面板定义,因为我无法访问堆栈面板元素无法实现我所知道的......

标签: xaml windows-phone-7 user-controls progress-bar


【解决方案1】:

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

 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>

【讨论】:

    【解决方案2】:

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

    popUp = new Popup();
    loginControl = new LoginPopup();  // this is the custom UserControl
    
    popUp.Child = loginControl;
    LayoutRoot.Children.Add(popUp);
    

    【讨论】:

      【解决方案3】:

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

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

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

      【讨论】:

      • 我找到了一个技巧 :) 并且 orinetation 工作正常.. 很快就会发布答案
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多