【问题标题】:The type or namespace name could not be found (New to XAML)找不到类型或命名空间名称(XAML 的新功能)
【发布时间】:2012-09-03 08:14:17
【问题描述】:

所以我正在尝试使用 Visual Studio 2012 在 XAML 文档上运行一个非常简单的骰子应用程序,但我不知道如何处理该错误。我尝试使用位图图像添加;但是还是不行。

这是我遇到的错误:

Error   1   Undefined namespace. The 'using' URI refers to a namespace 'DiceRoll' that could not be found.
Error   2   The type or namespace name 'BitmapImage' could not be found (are you missing a using directive or an assembly reference?)

CS:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using BitmapImage;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace DiceRoll
{
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainPage : Page
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Random num = new Random();
            int Number = num.Next(1, 7);
            BitmapImage Img = new BitmapImage(new Uri(@"DiceFaces\" + Number.ToString() + ".bmp", UriKind.Relative));

            textBlock1.Text = Number.ToString() + " Number";
            image1.Source = Img;


        }
    }
}

XAML:

<Page
    x:Class="DiceRoll.MainPage"
    IsTabStop="false"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:DiceRoll"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid>
        <TextBlock Height="56" HorizontalAlignment="Left" Margin="275,134,0,0" Name="textBlock1"  VerticalAlignment="Top" Width="182" />
        <Button Content="Roll a Dice" Height="23" HorizontalAlignment="Left" Margin="194,151,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
        <Image Height="72" HorizontalAlignment="Left" Margin="195,56,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="74" />
    </Grid>


</Page>

【问题讨论】:

    标签: c# xml xaml visual-studio-2012 xml-namespaces


    【解决方案1】:

    试试

    using System.Windows.Media.Imaging.BitmapImage;
    

    而不是

    using BitmapImage;
    

    更简单的方法是让 Visual Studio 为您完成: 右键单击代码中的 BitmapImage -> 解析,然后选择正确的命名空间(一般只有一个)

    对于 xaml,让 Visual Studio 也为您完成,或者:

    <Page ...
        xmlns:local="clr-namespace:System.Windows;assembly=DiceRoll"
    />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-12
      • 2011-05-13
      • 2013-03-25
      • 1970-01-01
      • 2012-06-19
      • 2017-11-29
      • 2012-09-27
      相关资源
      最近更新 更多