-1

image1 、WPFのc#で画像ソースを変更するには?

<Image 
            Visibility="Visible"
           Name="image1"
            Source="/Panorama-Kocka;component/slike/kocka.PNG"
            Grid.Column="1"
            Grid.Row="0" 
            Stretch="Fill"
            />
4

1 に答える 1

1

あなたと同じ問題を抱えて読んだ後、私は解決策を発見しました-パックURI。

コードで次のことを行いました。

Image finalImage = new Image();
finalImage.Width = 80;
...
BitmapImage logo = new BitmapImage()
logo.BeginInit();
logo.UriSource = new Uri("pack://application:,,,/ApplicationName;component/Resources/logo.png");
logo.EndInit();
...
finalImage.Source = logo;

URI は次の部分に分けられます。

Authority: application:///

Path: The name of a resource file that is compiled into a referenced assembly. The path must conform to the following format: AssemblyShortName[;Version][;PublicKey];component/Path
    AssemblyShortName: the short name for the referenced assembly.
    ;Version [optional]: the version of the referenced assembly that contains the resource file. This is used when two or more referenced assemblies with the same short name are loaded.
    ;PublicKey [optional]: the public key that was used to sign the referenced assembly. This is used when two or more referenced assemblies with the same short name are loaded.
    ;component: specifies that the assembly being referred to is referenced from the local assembly.
    /Path: the name of the resource file, including its path, relative to the root of the referenced assembly's project folder.

アプリケーションの後の 3 つのスラッシュ: はコンマに置き換える必要があります。

Note: The authority component of a pack URI is an embedded URI that points to a package and must conform to RFC 2396. Additionally, the "/" character must be replaced with the "," character, and reserved characters such as "%" and "?" must be escaped. See the OPC for details.

そしてもちろん、イメージのビルド アクションを Resource に設定してください。

于 2012-09-01T11:33:09.893 に答える