如何在windows手机中实现选择照片功能

时间:2014-10-01 06:16:34

标签: c# xaml windows-phone-8 windows-phone-8.1 scrollviewer

我正在开发一款应用,它需要从图库中选择背景图片。为此,我在Choose Photo中实现与Windows phone 8.1功能相同的功能(设置背景图片),

我试过这个:

XAML:

<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <Grid Grid.Row="0" Name="contentPanel">
        <ScrollViewer Name="scrl"  HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" Opacity="0.3">

        </ScrollViewer>
        <ScrollViewer Name="scrlView" Height="500" Width="300" BorderBrush="Red" BorderThickness="1" Background="Transparent">

        </ScrollViewer>
        <Image Name="mtpImg" Stretch="Fill" />
    </Grid>
</Grid>

<phone:PhoneApplicationPage.ApplicationBar>
    <shell:ApplicationBar Mode="Minimized">
        <shell:ApplicationBarIconButton IconUri="Assets\ApplicationIcon.png" Click="gallery_click" Text="gallery"/>
    </shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>

C#:

    private void gallery_click(object sender, EventArgs e)
    {
        PhotoChooserTask chooser = new PhotoChooserTask();
        chooser.Completed += gallery_Completed;
        chooser.Show();
    }

    private void gallery_Completed(object sender, PhotoResult e)
    {
        if (e.TaskResult == TaskResult.OK)
        {
            Image img = new Image();
            BitmapImage tmpBitmap = new BitmapImage();
            tmpBitmap.SetSource(e.ChosenPhoto);
            img.Source = tmpBitmap;
            scrl.Content = img;
        }
    }

问题:如何为opacity=1 ScrollViewer中的图片设置scrlView

1 个答案:

答案 0 :(得分:1)

尝试将此不透明度设置为1:

private void gallery_Completed(object sender, PhotoResult e)
{
    if (e.TaskResult == TaskResult.OK)
    {
        Image img = new Image();
        BitmapImage tmpBitmap = new BitmapImage();
        tmpBitmap.SetSource(e.ChosenPhoto);
        img.Source = tmpBitmap;
        scrl.Content = img;
        scrl.Opacity = 1.0;
    }
}

如果您想在带有边框的图像控件中设置图像您可以尝试:

XAML:

<Grid Grid.Row="0" Name="contentPanel">
    <Border BorderBrush="Red" Height="500" Width="300" BorderThickness="1">
        <Image Name="mtpImg" Stretch="Fill" Height="500" Width="300"/>
    </Border>
</Grid>

CS:

PhotoChooserTask chooser;
public TaskPage()
{
    InitializeComponent();
    chooser = new PhotoChooserTask();
    chooser.Completed += gallery_Completed;
}

private void gallery_click(object sender, EventArgs e)
{
    chooser.Show();
}

private void gallery_Completed(object sender, PhotoResult e)
{
    if (e.TaskResult == TaskResult.OK)
    {
        BitmapImage tmpBitmap = new BitmapImage();
        tmpBitmap.SetSource(e.ChosenPhoto);
        mtpImg.Source = tmpBitmap;
    }
}