编辑现有WP8项目时出错

时间:2014-03-16 03:35:03

标签: c# visual-studio-2010 xaml windows-phone-8 compiler-errors

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

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

  

错误1类型' SoundBoard.MainPage'已经包含一个定义   对于' LayoutRoot' \音板\ OBJ \调试\ Cartoons.g.cs

     

错误30名称' AudioPlayer'在当前不存在   context \ SoundBoard \ Cartoons.xaml.cs

     

错误7键入' SoundBoard.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();
        }

    }
}

正如我所说,它可能是愚蠢的但我真的被卡住了所以非常感谢任何建议。

提前致谢。

2 个答案:

答案 0 :(得分:1)

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

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

当它存在时,抱怨AudioPlayer不存在,在Cartoons.xaml中声明。这就是所有奇怪的错误。

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

答案 1 :(得分:1)

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