0

Visual Studio Community 2015 を使用して UWP に取り組んでいます。空のプロジェクトを作成し、ボタンと RichEditBox を追加しました。ローカル リソースから RichEditBox に画像を挿入しようとしていますが、空白のプレースホルダー (目的のサイズの画像) のみを挿入します。エラーはスローされません。

コードビハインドは次のとおりです。

private async void Button_Click(object sender, RoutedEventArgs e)
{
    Uri imageUri = new Uri("ms-appx:///Assets/StoreLogo.png");
    using (IRandomAccessStream ras = await RandomAccessStreamReference.CreateFromUri(imageUri).OpenReadAsync())
    {
        box.Document.Selection.InsertImage(64, 64, 0, VerticalCharacterAlignment.Baseline, "img", ras);
    }
}

xaml は次のとおりです。

<Button Content="Insert image" Click="Button_Click"/>
<RichEditBox Name="box" Height="200"/>

StoreLogo.png のビルド アクションは「コンテンツ」です。イメージを出力ディレクトリにコピーしようとしましたが、違いはありませんでした。

ここで何が問題になる可能性がありますか?これを行う正しい方法は何ですか?

4

1 に答える 1

2

私はあなたの問題を見ることができますが、私の経験では、一般的な方法はStorageFile.OpenAsyncget を使用することIRandomAccessStreamです。に設定しITextRange.InsertImageます。

例えば:

Uri imageUri = new Uri("ms-appx:///Assets/StoreLogo.png");

StorageFile imageFile = await StorageFile.GetFileFromApplicationUriAsync(imageUri);

using (IRandomAccessStream fileStream = await imageFile.OpenAsync(Windows.Storage.FileAccessMode.Read))

{

    box.Document.Selection.InsertImage(64, 64, 0, VerticalCharacterAlignment.Baseline, "img", fileStream);

}

この方法を使用して回避することができます。

于 2016-03-21T11:49:01.620 に答える