如何在自定义消息框中显示图像

时间:2014-07-25 05:04:39

标签: c# image windows-phone-8 messagebox

我需要将图像显示为CustomMessageBox的内容。我试图按如下方式设置它,但是没有显示图像,但其他一切看起来都没问题。

Image image = new Image();
BitmapImage bmp = new BitmapImage(new Uri("/Assets/image.png", UriKind.Relative));
image.Source = bmp as ImageSource;

CustomMessageBox messageBox = new CustomMessageBox()
{
    Caption = "\n" + "Caption",
    Message = "\n" + "Message. + "\n",
    LeftButtonContent = "left button",
    Content = image
};

messageBox.Dismissed += (s1, e1) =>
{
    switch (e1.Result)
    {
        case CustomMessageBoxResult.LeftButton:
            MessageBox.Show("left button");
            break;
        case CustomMessageBoxResult.None:
            MessageBox.Show("none");
            break;
        default:
            MessageBox.Show("default");
            break;
    }
};

messageBox.Show();

3 个答案:

答案 0 :(得分:1)

您可以添加自定义用户界面作为自定义消息框的内容。我已经做了。这是我的一个项目的代码。它在StackPanel的旁边有一个图像和一个textBox。以下是创建UI的方法。

private StackPanel CreateUI(string imagePath, string username)
{
    StackPanel userStack = new StackPanel()
    {
        Orientation = System.Windows.Controls.Orientation.Horizontal,
        HorizontalAlignment = System.Windows.HorizontalAlignment.Left,
        Margin = new Thickness(36, 24, 0, 0)
    };

    Image profilePic = new Image()
    {
        Source = new BitmapImage(new Uri(imagePath, UriKind.Absolute)),
        Name = "imgProfile",
        Height = 100,
        Width = 100,
        Margin = new Thickness(0, 0, 6, 0)
    };

    TextBlock userName = new TextBlock()
    {
        Text = username,
        Name = "txblkUserName",
        Foreground = new SolidColorBrush(Colors.White),
        FontSize = 32,
        Margin = new Thickness(0, 12, 0, 0)
    };


    userStack.Children.Add(profilePic);
    userStack.Children.Add(userName);
    return userStack;
}

以下是我将它添加到CustomMessageBox的方法。

CustomMessageBox msgBox = new CustomMessageBox()
{
    Caption = "Your Caption",
    Content = this.CreateUI(profilePic, userName),
    Message = "Your Message",
    LeftButtonContent = "Left Button Content"
};
msgBox.Show();

完美无缺。希望这会有所帮助:)

**编辑:如果您的图片在您的项目中,那么UriKind应该更改为相对。

答案 1 :(得分:0)

我不知道CustomMessageBox,但我知道它使用Popup来显示消息框。

你也可以这样做。这应该会给你更多的自由。

请参阅here http://developer.nokia.com/community/wiki/How_to_use_Pop-Ups_in_Windows_Phone

答案 2 :(得分:0)

在XAML中

<Popup Grid.Row="1"  x:Name="popup" IsOpen="False" Margin="0, -30,0,0">

            <Grid Background="Black" Height="300" Width="480">

                <StackPanel>
                    <TextBlock Text="MsgBox with Image" FontSize="40" Foreground="White" Margin="15 20"></TextBlock>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="This is an image -> " FontSize="22" Foreground="White" Margin="15 0 0 20"></TextBlock>
                        <Image Source="imagepath" Height="45" VerticalAlignment="Top" Margin="0 -10"/>
                        <TextBlock Text=" in a msgbox " FontSize="22" Foreground="White" Margin="15 0 0 20"></TextBlock>
                    </StackPanel>

                    <Button Content="ok" Width="220" Margin="5 40 20 0" HorizontalAlignment="Left" Click="OKButton_Click"/>
                </StackPanel>

            </Grid>

        </Popup>

在您的代码中:

this.popup.IsOpen = true; //opens our custom msgbox

            if (this.popup.IsOpen == true)
            {
                scrollviewerCustom.VerticalScrollBarVisibility = ScrollBarVisibility.Disabled; //disables the scrollviewer
                this.ApplicationBar.Disable(); //disables the application bar
            }



//on click of OK button
private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.popup.IsOpen = false; //closes our msgbox
            scrollviewerCustom.VerticalScrollBarVisibility = ScrollBarVisibility.Visible; //enables the scrollviewer
            this.ApplicationBar.Enable(); //enables the application bar
        }