質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
.NET Standard

.NET Standardは、さまざまな.NET環境で用いることができる基本的なAPIセットまたはそれを定めた仕様です。この標準に沿って.NET実装を行うことで、どのプラットフォームでも動くポータブルなライブラリを作成できます。

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

Q&A

解決済

2回答

1864閲覧

ユーザーコントロールから、他プロジェクトにある画像の参照方法について

tride

総合スコア68

.NET Standard

.NET Standardは、さまざまな.NET環境で用いることができる基本的なAPIセットまたはそれを定めた仕様です。この標準に沿って.NET実装を行うことで、どのプラットフォームでも動くポータブルなライブラリを作成できます。

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

0グッド

0クリップ

投稿2019/11/11 00:55

編集2019/11/12 00:37

表題の件について、スタイルが設定されているButton_Jpa.xamlが他プロジェクトにある画像ファイルへのパス指定ができずで困っております。

ファイルの読み取りの流れとしては、MainWindows.xamlからユーザーコントロールのPage_Top.xamlを読み出し、Page_Top.xamlからリソースディクショナリのButton_Jpa.xamlを読みにいく仕様になっております。

そして、Button_Jpa.xamlのControlTemplate.Triggersにて他のプロジェクトの画像ファイルを指定しております。

エラー個所はMainWindowの

<エラー内容>
リソース 'resources/images/japanese/Button0_jpa.png' を検索できません。

<対象と思われる個所>

xaml

1<!--<Button_Jpa.xaml>--> 2 <BitmapImage x:Key="ImageSource_0" UriSource="pack://application:,,,/AppCore;component/Resources/Images/Japanese/Button0_jpa.png" /> 3 <BitmapImage x:Key="ImageSource_1" UriSource="pack://application:,,,/AppCore;component/Resources/Images/Japanese/Button1_jpa.png" /> 4 <BitmapImage x:Key="ImageSource_2" UriSource="pack://application:,,,/AppCore;component/Resources/Images/Japanese/Button2_jpa.png" /> 5 <BitmapImage x:Key="ImageSource_3" UriSource="pack://application:,,,/AppCore;component/Resources/Images/Japanese/Button3_jpa.png" /> 6

<環境>
VisualStudio2019

<ソリューション構成>
プロジェクト:AppCore(.Net Standardクラスライブラリ)
----- ┗Resources
---------- ┗Images
--------------- ┗Japanese
-------------------- ┗Button*_jpa.png(ビルドアクション:リソース)
※画像ファイルの番号については省略

プロジェクト:AppWPF(WPFアプリ.Net Freamwork / AppCoreを参照)
----- ┣MainWindow.xaml
----- ┣Style
----- ┃ ┗Japanese
----- ┃ ┗Button_Jpa.xaml
----- ┗UserControlObjects
---------- ┗Page
---------- ┗Page_Top.xaml

<以下コード全文>

xaml

1<!--MainWindow.xaml--> 2<Window x:Class="AppWPF.MainWindow" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 6 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 7 xmlns:page="clr-namespace:AppWPF.UserControlObjects.Page" 8 mc:Ignorable="d" 9 Title="MainWindow" Height="900" Width="1440"> 10 11 12 <Grid> 13 <StackPanel> 14 15 <!-- コンテンツ --> 16 <StackPanel x:Name="ContentsColum" Margin="0" Height="869" VerticalAlignment="Stretch"> 17 <page:Page_Top VerticalAlignment="Stretch" Margin="0,0,0,0"/> ←エラー個所 18 </StackPanel> 19 </StackPanel> 20 </Grid> 21</Window>

xaml

1<!--<Page_Top.xaml>--> 2<UserControl x:Class="AppWPF.UserControlObjects.Page.Page_Top" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 7 mc:Ignorable="d" 8 Stylus.IsPressAndHoldEnabled="False" HorizontalAlignment="Center" VerticalAlignment="Center"> 9 10 11 <UserControl.Resources> 12 <ResourceDictionary> 13 <ResourceDictionary.MergedDictionaries> 14 <ResourceDictionary Source="/AppWpf;component/Style/Japanese/Button_Jpa.xaml"/> 15 </ResourceDictionary.MergedDictionaries> 16 </ResourceDictionary> 17 </UserControl.Resources> 18 19 <StackPanel x:Name="CanvasTop" Margin="0,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Center"> 20 <StackPanel x:Name="HeaderColum" Orientation="Horizontal" Height="150" ></StackPanel> 21 <StackPanel x:Name="ContentsColum" Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center" > 22 <Button x:Name="ButtonTest" HorizontalAlignment="Center" VerticalAlignment="Center" Width="400" Height="400" Template="{StaticResource ResourceKey=Template_ModeButton}" Cursor="Hand"/> 23 </StackPanel> 24 </StackPanel> 25</UserControl>

xaml

1<!--<Button_Jpa.xaml>--> 2<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 4 5<!-- 分からないところ -->> 6 <BitmapImage x:Key="ImageSource_0" UriSource="pack://application:,,,/AppCore;component/Resources/Images/Japanese/Button0_jpa.png" /> 7 <BitmapImage x:Key="ImageSource_1" UriSource="pack://application:,,,/AppCore;component/Resources/Images/Japanese/Button1_jpa.png" /> 8 <BitmapImage x:Key="ImageSource_2" UriSource="pack://application:,,,/AppCore;component/Resources/Images/Japanese/Button2_jpa.png" /> 9 <BitmapImage x:Key="ImageSource_3" UriSource="pack://application:,,,/AppCore;component/Resources/Images/Japanese/Button3_jpa.png" /> 10 11 12 <!-- ボタン --> 13 <ControlTemplate x:Key="Template_ModeButton" TargetType="{x:Type Button}"> 14 <!-- 標準時の画像を表示 --> 15 <Border x:Name="border" BorderThickness="0" BorderBrush="Transparent" CornerRadius="200"> 16 <Border.Background> 17 <ImageBrush ImageSource="{StaticResource ImageSource_1}" /> 18 </Border.Background> 19 <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" /> 20 </Border> 21 22 23 <ControlTemplate.Triggers > 24 <!-- ボタン無効時 --> 25 <Trigger Property="IsEnabled" Value="False"> 26 <Setter TargetName="border" Property="Background" > 27 <Setter.Value> 28 <ImageBrush ImageSource="{StaticResource ImageSource_0}" /> 29 </Setter.Value> 30 </Setter> 31 </Trigger> 32 33 <!-- マウスーバー時の画像を表示 --> 34 <Trigger Property="IsMouseOver" Value="true"> 35 <Setter TargetName="border" Property="Background" > 36 <Setter.Value> 37 <ImageBrush ImageSource="{StaticResource ImageSource_2}" /> 38 </Setter.Value> 39 </Setter> 40 </Trigger> 41 42 <!-- クリック時(長押し)時の画像 --> 43 <!-- Stylus.IsPressAndHoldEnabled="False"をWindowタグまたはButtonタグに追記 --> 44 <Trigger Property="IsPressed" Value="True" > 45 <Setter TargetName="border" Property="Background" > 46 <Setter.Value> 47 <ImageBrush ImageSource="{StaticResource ImageSource_3}" /> 48 </Setter.Value> 49 </Setter> 50 </Trigger> 51 </ControlTemplate.Triggers> 52 53 </ControlTemplate> 54</ResourceDictionary> 55

2019/11/12追記:
再現しないと回答をいただきましたので、新たにAppResources(カスタムコントロールライブラリ)としてプロジェクトを追加作成し、パスを以下に変更すると実行できました。

<BitmapImage x:Key="ImageSource_0" UriSource="pack://application:,,,/AppResources;component/Resources/Images/Japanese/Button0_jpa.png" />

その為、AppCoreの構成が間違っているのではないかと推測が立ちました。

AppCoreの構成については、.Net Standard 2.0のクラスライブラリ(以前でいうPCL)で作っておりました。
関係していると思っていなかったので省いていましたが、なにやらこの辺りに原因がありそうです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Zuishin

2019/11/12 00:31

実行できたのなら解決したのではありませんか?
tride

2019/11/12 00:34

現在のAppCore(.Net Stunderd)で達成したい為、解決にはしておりません。
Zuishin

2019/11/12 00:44

WPF は .NET Standard にはありません。
Zuishin

2019/11/12 00:49

.NET Standard を諦めるか、たとえば API を作るなど別の方法で画像を取得してください。
Zuishin

2019/11/12 00:53

画像をアセンブリに入れるのではなく、ファイルとして提供するという方法もあります。
Zuishin

2019/11/12 00:55

カスタムコントロールライブラリにしただけで、他のものは一切変えていないなら、そこに原因があることは明白で、この質問は解決したように思います。 他の手段はたくさんあるので、これは自分で方針を決めて実装し、わからないところがあれば改めて質問してください。
guest

回答2

0

単純にWPFは.NET Standard にないという事を知りませんでした。
ご指摘の通り、プロジェクトを再度作成する事で解決とします。

投稿2019/11/13 01:33

tride

総合スコア68

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

再現しません。画像のビルドアクションがすべてリソースになっていることを確認し、リビルドしてください。それでできなければ、ここに掲載されたコード以外の部分に問題があるものと思います。

投稿2019/11/11 13:59

Zuishin

総合スコア28660

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Zuishin

2019/11/11 14:27

エラー一覧を表示して IsFrozen 云々書かれていた場合、BitmapImage ではなく ImageBrush を ResourceDictionary に入れてみたください。
tride

2019/11/12 00:32

エラー一覧にはIsFrozen云々はなく、記載にあるエラーのみです。 また、再現しないという回答をいただいたので、対応した結果を追記しました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問