编辑现有WP8项目时出现错误

本文关键字:错误 WP8 编辑 项目 | 更新日期: 2023-09-27 17:50:23

所以我刚开始用这些东西,遇到了一些冲突。我需要将一个枢轴音板应用程序更改为一系列全景页面,但具有相同的功能。

我已经适当地完成了所有的xaml和c#,但我认为,因为我必须创建一个新的MainPage并删除旧的MainPage,我已经创建了几个错误。我怀疑他们都有相似的原因,所以我只提供三个。

错误1类型为"SoundBoard"。MainPage'已经包含了一个定义for 'LayoutRoot' 'SoundBoard'obj'Debug'卡通。g.cs

错误30名称"AudioPlayer"在当前不存在上下文'响板' Cartoons.xaml.cs

错误7输入"音板"。MainPage'已经定义了一个名为'InitializeComponent'使用相同的参数类型音板' ' obj '调试' Cartoons.g.cs

我会把我现在的主页和卡通链接起来,希望你们能帮助我。

MainPage.xaml:

<phone:PhoneApplicationPage
    x:Class="SoundBoard.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DataContext="{d:DesignData SampleData/SampleData.xaml}"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait"  Orientation="Portrait"
    shell:SystemTray.IsVisible="True">
    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel Grid.Row="0" Margin="12,17,0,28">
            <TextBlock Text="SoundBoard" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock Text="Categories" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>
        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Button Content="Warnings" HorizontalAlignment="Left" Margin="244,304,0,0" VerticalAlignment="Top" Height="160" Width="182" Background="Red" Click="Button_Click_3"/>
            <Button Content="Taunts" HorizontalAlignment="Left" Margin="27,304,0,0" VerticalAlignment="Top" Height="160" Width="182" Background="Red" Click="Button_Click_2"/>
            <Button Content="Cartoons" HorizontalAlignment="Left" Margin="244,123,0,0" VerticalAlignment="Top" Height="160" Width="182" Background="Red" Click="Button_Click_1"/>
            <Button Content="Animals" HorizontalAlignment="Left" Margin="27,123,0,0" VerticalAlignment="Top" Height="160" Width="182" Background="Red" Click="Button_Click"/>
        </Grid>
    </Grid>
</phone:PhoneApplicationPage>

MainPage.xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
namespace SoundBoard
{
    public partial class FirstPage : PhoneApplicationPage
    {
        public FirstPage()
        {
            InitializeComponent();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Animals.xaml", UriKind.Relative));
        }
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Cartoons.xaml", UriKind.Relative));
        }
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Taunts.xaml", UriKind.Relative));
        }
        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Warnings.xaml", UriKind.Relative));
        }
    }
}

Cartoons.xaml:

<phone:PhoneApplicationPage
    x:Class="SoundBoard.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DataContext="{d:DesignData SampleData/SampleData.xaml}"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait"  Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <!--LayoutRoot contains the root grid where all other page content is placed-->
    <Grid x:Name="LayoutRoot">
        <phone:Panorama Title="{Binding Path=LocalizedResources.ApplicationTitle,
            Source={StaticResource LocalizedStrings}}">
            <!--Panorama item one-->
            <phone:PanoramaItem Header="Cartoons">
                <Grid>
                    <Image HorizontalAlignment="Center" Height="431" Margin="10,10,0,0" VerticalAlignment="Top" Width="400" Source="/Assets/cartoon.jpg"/>
                </Grid>
            </phone:PanoramaItem>
            <!--Panorama item two-->
            <phone:PanoramaItem Header="Sounds">
                <phone:LongListSelector
                    Margin="0,0,-12,0" 
                    ItemsSource="{Binding Cartoons.Items}"
                    LayoutMode="Grid"
                    GridCellSize="150,150"                  
                    >
                    <phone:LongListSelector.ItemTemplate>
                        <DataTemplate>
                            <Grid Background="{StaticResource PhoneAccentBrush}">
                                <Grid VerticalAlignment="Top" HorizontalAlignment="Right" 
                                      Width="40" Height="40" Margin="0,6,6,0">
                                    <Ellipse Stroke="{StaticResource PhoneForegroundBrush}" 
                                             StrokeThickness="3" />
                                    <Image Source="Assets/AppBar/Play.png" />

                                </Grid>
                                <StackPanel VerticalAlignment="Bottom">
                                    <TextBlock Text="{Binding Title}"
                                               Margin="6,0,0,6"/>
                                </StackPanel>
                            </Grid>
                        </DataTemplate>
                    </phone:LongListSelector.ItemTemplate>
                </phone:LongListSelector>
            </phone:PanoramaItem>
            <!--Panorama item three-->
            <phone:PanoramaItem Header="Record">
                <Grid>
                    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
                        <MediaElement x:Name="AudioPlayer" AutoPlay="False" />
                        <StackPanel>
                            <ToggleButton Content="Record"
                                          Checked="RecordAudioChecked"
                                          Unchecked="RecordAudioUnchecked"/>
                            <Grid Width="200" 
                                  Height="200" 
                                  Name="ReelGrid"
                                  RenderTransformOrigin=".5,.5">
                                <Grid.RenderTransform>
                                    <CompositeTransform />
                                </Grid.RenderTransform>

                                <Ellipse Fill="{StaticResource PhoneAccentBrush}" />
                                <Ellipse Height="20"
                                         Width="20"
                                         Fill="{StaticResource PhoneForegroundBrush}" />
                                <Rectangle Height="20"
                                           Width="20"
                                           Margin="0,20,0,20"
                                           VerticalAlignment="Top" 
                                           Fill="{StaticResource PhoneForegroundBrush}" />
                                <Rectangle Height="20"
                                           Width="20"
                                           Margin="0,20,0,20"
                                           VerticalAlignment="Bottom" 
                                           Fill="{StaticResource PhoneForegroundBrush}" />
                                <Rectangle Height="20"
                                           Width="20"
                                           Margin="0,0,20,0"
                                           HorizontalAlignment="Right" 
                                           Fill="{StaticResource PhoneForegroundBrush}" />
                                <Rectangle Height="20"
                                           Width="20"
                                           Margin="20,0,0,0"
                                           HorizontalAlignment="Left" 
                                           Fill="{StaticResource PhoneForegroundBrush}" />
                            </Grid>
                            <Button Name="PlayAudio" Content="Play" Click="PlayAudioClick" />
                        </StackPanel>
                    </Grid>
                </Grid>
            </phone:PanoramaItem>
        </phone:Panorama>
    </Grid>
</phone:PhoneApplicationPage>

Cartoons.xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using SoundBoard.Resources;
using Coding4Fun.Toolkit.Audio;
using Coding4Fun.Toolkit.Audio.Helpers;
using System.IO;
using System.IO.IsolatedStorage;
using Coding4Fun.Toolkit.Controls;
using SoundBoard.ViewModels;
using Newtonsoft.Json;
namespace SoundBoard
{
    public partial class Cartoons : PhoneApplicationPage
    {
        private MicrophoneRecorder _recorder = new MicrophoneRecorder();
        private IsolatedStorageFileStream _audioStream;
        private string _tempFileName = "tempWav.wav";
        public Cartoons()
        {
            InitializeComponent();
            DataContext = App.ViewModel;
            BuildLocalizedApplicationBar();
        }
            // Load data for the ViewModel Items
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
            }
        }
        private void LongListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            LongListSelector selector = sender as LongListSelector;
            // verifying our sender is actually a LongListSelector
            if (selector == null)
                return;
            SoundData data = selector.SelectedItem as SoundData;
            // verifying our sender is actually SoundData
            if (data == null)
                return;
            if (File.Exists(data.FilePath))
            {
                AudioPlayer.Source = new Uri(data.FilePath, UriKind.RelativeOrAbsolute);
            }
            else
            {
                using (var storageFolder = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (var stream = new IsolatedStorageFileStream(data.FilePath, FileMode.Open, storageFolder))
                    {
                        AudioPlayer.SetSource(stream);
                    }
                }
            }
            selector.SelectedItem = null;
        }
        private void BuildLocalizedApplicationBar()
        {
            ApplicationBar = new ApplicationBar();
            ApplicationBarIconButton recordAudioAppBar =
                new ApplicationBarIconButton();
            recordAudioAppBar.IconUri = new Uri("/Assets/AppBar/Save.png", UriKind.Relative);
            recordAudioAppBar.Text = AppResources.AppBarSave;
            recordAudioAppBar.Click += recordAudioAppBar_Click;
            ApplicationBar.Buttons.Add(recordAudioAppBar);
            ApplicationBar.IsVisible = false;
        }
        void recordAudioAppBar_Click(object sender, EventArgs e)
        {
            InputPrompt fileName = new InputPrompt();
            fileName.Message = "What should we call the sound?";
            fileName.Completed += FileNameCompleted;
            fileName.Show();
        }
        private void FileNameCompleted(object sender, PopUpEventArgs<string, PopUpResult> e)
        {
            if (e.PopUpResult == PopUpResult.Ok)
            {
                // Create a sound data object
                SoundData soundData = new SoundData();
                soundData.FilePath = string.Format("/customAudio/{0}.wav", DateTime.Now.ToFileTime());
                soundData.Title = e.Result;
                // Save the wav file into the DIR /customAudio/
                using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication()) {
                    if (!isoStore.DirectoryExists("/customAudio/"))
                        isoStore.CreateDirectory("/customAudio/");
                    isoStore.MoveFile(_tempFileName, soundData.FilePath);
                }

                // Add the SoundData to App.ViewModel.CustomSounds
                App.ViewModel.CustomSounds.Items.Add(soundData);
                // Save the list of CustomSounds to isolatedStorage.ApplicationSettings
                var data = JsonConvert.SerializeObject(App.ViewModel.CustomSounds);
                IsolatedStorageSettings.ApplicationSettings[SoundModel.CustomSoundKey] = data;
                IsolatedStorageSettings.ApplicationSettings.Save();
                // We'll need to modify sound model to retrieve CustomSounds
                //from isolatedStorage.ApplicationSettings
                NavigationService.Navigate(new Uri("", UriKind.RelativeOrAbsolute));
            }
        }
        private void RecordAudioChecked(object sender, RoutedEventArgs e)
        {
            PlayAudio.IsEnabled = false;
            ApplicationBar.IsVisible = false;
            RotateCirlce.Begin();
            _recorder.Start();
        }
        private void RecordAudioUnchecked(object sender, RoutedEventArgs e)
        {
            _recorder.Stop();
            SaveTempAudio(_recorder.Buffer);
            PlayAudio.IsEnabled = true;
            ApplicationBar.IsVisible = true;
            RotateCircle.Stop();
        }
        private void SaveTempAudio(MemoryStream buffer)
        {
            //defensive...
            if (buffer == null)
                throw new ArgumentNullException("Attempting a save on empty sound buffer.");
            //Clean out hold on audioStream
            if (_audioStream != null)
            {
                AudioPlayer.Stop();
                AudioPlayer.Source = null;
                _audioStream.Dispose();
            }
            var bytes = buffer.GetWavAsByteArray(_recorder.SampleRate);
            using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication()) {
                if (isoStore.FileExists(_tempFileName))
                    isoStore.DeleteFile(_tempFileName);
                _tempFileName = string.Format("{0}.wav", DateTime.Now.ToFileTime());

                _audioStream = isoStore.CreateFile(_tempFileName);
                _audioStream.Write(bytes, 0, bytes.Length);
                //Play ...  SetSource of a mediaElement
                AudioPlayer.SetSource(_audioStream);
            }
        }
        private void PlayAudioClick(object sender, RoutedEventArgs e)
        {
            AudioPlayer.Play();
        }
    }
}

正如我所说的,这可能是一些愚蠢的东西,但我真的卡住了,所以我会非常感谢任何建议。

编辑现有WP8项目时出现错误

似乎所有的问题都存在于自动生成的文件Cartoons.g.cs。指向非完全自动生成文件的唯一错误是:

The name 'AudioPlayer' does not exist in the current context 'SoundBoard'Cartoons.xaml.cs

AudioPlayer的抱怨不存在,而它确实存在,在cartoon .xaml中声明。这些都是奇怪的错误。

我要做的第一件事是清理(从visual studio,或手动删除bindebug文件夹)和重建项目,甚至重新启动visual studio。这些步骤在大多数情况下可以修复奇怪的错误。

问题是Cartoons.xamlMainPage.xaml都有同一行x:Class="SoundBoard.MainPage"。我不确定,但认为Cartoons.xaml应该对x:Class属性有不同的值