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

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

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

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

WPF

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

Q&A

解決済

1回答

5294閲覧

WPFのWindow継承でエラーが出ます

Lyricania0220

総合スコア11

C#

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

WPF

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

1グッド

0クリップ

投稿2019/12/22 05:43

前提・実現したいこと

WPFにて、namespaceの異なるクラスを継承したい。

発生している問題・エラーメッセージ

MainWindowのXAMLにて、下記のエラーが発生しております。

'x:Class' は、ルート タグでのみ指定できます

該当のソースコード

C#

1using WindowTest.UI; 2namespace WindowTest 3{ 4 public partial class MainWindow : WindowBase2 5 { 6 public MainWindow() 7 { 8 InitializeComponent(); 9 } 10 } 11}

XAML

1<local:WindowTest.UI.WindowBase2 x:Class="WindowTest.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:WindowTest" 7 mc:Ignorable="d" 8 Title="MainWindow" Height="450" Width="800"> 9</local:WindowTest.UI.WindowBase2> 10

C#

1using System.Windows; 2namespace WindowTest 3{ 4 public class WindowBase1 : Window 5 { 6 protected string GetStr() 7 { 8 return "Test"; 9 } 10 } 11}
using System.Windows; namespace WindowTest.UI { public class WindowBase2 : Window { protected string GetStr() { return "Test"; } } }

試したこと

MainWindowと同じnamespaceのWindowBase1を基底クラスにしたときはエラーは出ませんでした。

補足情報(FW/ツールのバージョンなど)

Visual Studio 2019
.Net Framework 4.7.2

TN8001👍を押しています

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

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

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

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

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

guest

回答1

0

ベストアンサー

xml

1<ui:WindowBase2 2 x:Class="WindowTest.MainWindow" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 xmlns:ui="clr-namespace:WindowTest.UI" 6 xmlns:local="clr-namespace:WindowTest"> 7</ui:WindowBase2>

xmlns:ui="clr-namespace:WindowTest.UI"のようにxml名前空間を指定して、ui:WindowBase2と書くことになります。uiは任意の名前で結構です。


xamlのルートのクラスを変更すると、実行はできるのにデザイナがエラーを吐くことがよくあります。
objやbinフォルダを削除したりVisualStudioを再起動したりしてなだめているんですが、この直し方は私も知りたいです^^;

投稿2019/12/22 06:53

編集2023/07/17 13:02
TN8001

総合スコア9321

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

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

Lyricania0220

2019/12/22 07:00

ありがとうございます! おかげで解決できました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問