在WP7中更改用户控件(ProgressBar)的方向?

时间:2011-11-18 16:28:37

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

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

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>

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

3 个答案:

答案 0 :(得分:2)

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

 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>

答案 1 :(得分:2)

我可以通过简单地添加主屏幕的子项来启用弹出式UserControl的方向,其中Popup显示为:

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

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

答案 2 :(得分:1)

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

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

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